1 /* Server for the Midnight Commander Virtual File System.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
7 Miguel de Icaza, 1995, 1997,
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * \brief Source: server for the Midnight Commander Virtual File System
28 * \author Miguel de Icaza
29 * \author Andrej Borsenkow
30 * \date 1995, 1996, 1997
32 * \todo opendir instead of keeping its table of file handles could return
33 * the pointer and expect the client to send a proper value back each
34 * time :-) We should use syslog to register login/logout.
37 /* {{{ Includes and global variables */
50 # include <sys/param.h>
52 # define NGROUPS_MAX NGROUPS
56 #include <sys/types.h>
65 /* Network include files */
66 #include <sys/socket.h>
67 #include <netinet/in.h>
69 #ifdef HAVE_ARPA_INET_H
70 #include <arpa/inet.h>
74 # include <rpc/pmap_prot.h>
75 # ifdef HAVE_RPC_PMAP_CLNT_H
76 # include <rpc/pmap_clnt.h>
81 # if !defined(HAVE_SECURITY_PAM_MISC_H)
86 /* Authentication include files */
89 # include <security/pam_misc.h>
90 # ifndef PAM_ESTABLISH_CRED
91 # define PAM_ESTABLISH_CRED PAM_CRED_ESTABLISH
94 #endif /* !HAVE_PAM */
98 #endif /* !HAVE_CRYPT_H */
103 # ifdef HAVE_SHADOW_SHADOW_H
104 # include <shadow/shadow.h>
109 * GNU gettext defines printf to libintl_printf on platforms that lack
110 * a native printf(3) capable of all POSIX features.
113 #include "../src/global.h"
115 #include "../src/wtools.h" /* message() */
116 #include "../src/main.h" /* print_vfs_message */
120 #include "mcfsutil.h"
124 # define INADDR_NONE (0xffffffffU)
127 /* replacement for g_free() from glib */
129 #define g_free(x) do {if (x) free (x);} while (0)
131 /* We don't care about SIGPIPE */
134 /* The socket from which we accept commands */
137 /* Requested version number from client */
138 static int clnt_version
;
140 /* If non zero, we accept further commands */
144 const char *home_dir
= NULL
;
148 /* Were we started from inetd? */
149 int inetd_started
= 0;
151 /* Are we running as a daemon? */
160 /* port number in which we listen to connections,
161 * if zero, we try to contact the portmapper to get a port, and
162 * if it's not possible, then we use a hardcoded value
166 /* if the server will use rcmd based authentication (hosts.equiv .rhosts) */
169 #define OPENDIR_HANDLES 8
171 #define DO_QUIT_VOID() \
178 /* Only used by get_port_number */
179 #define DO_QUIT_NONVOID(a) \
188 static int quit_server
;
189 static int return_code
;
193 /* {{{ Misc routines */
196 send_status (int status
, int errno_number
)
198 rpc_send (msock
, RPC_INT
, status
, RPC_INT
, errno_number
, RPC_END
);
204 /* {{{ File with handle operations */
209 int handle
, flags
, mode
;
212 rpc_get (msock
, RPC_STRING
, &arg
, RPC_INT
, &flags
, RPC_INT
, &mode
,
215 handle
= open (arg
, flags
, mode
);
216 send_status (handle
, errno
);
223 int handle
, count
, n
;
226 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
227 data
= malloc (count
);
229 send_status (-1, ENOMEM
);
233 printf ("count=%d\n", count
);
234 n
= read (handle
, data
, count
);
236 printf ("result=%d\n", n
);
238 send_status (-1, errno
);
242 rpc_send (msock
, RPC_BLOCK
, n
, data
, RPC_END
);
250 int handle
, count
, status
, written
= 0;
253 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
256 int nbytes
= count
> 8192 ? 8192 : count
;
258 rpc_get (msock
, RPC_BLOCK
, nbytes
, buf
, RPC_END
);
259 status
= write (handle
, buf
, nbytes
);
261 send_status (status
, errno
);
264 /* FIXED: amount written must be returned to caller */
266 if (status
< nbytes
) {
267 send_status (written
, errno
);
272 send_status (written
, errno
);
278 int handle
, offset
, whence
, status
;
280 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &offset
, RPC_INT
, &whence
,
282 status
= lseek (handle
, offset
, whence
);
283 send_status (status
, errno
);
291 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
292 status
= close (handle
);
293 send_status (status
, errno
);
298 /* {{{ Stat family routines */
301 send_time (int sock
, time_t t
)
303 if (clnt_version
== 1) {
308 ct
[3] = ct
[10] = ct
[13] = ct
[16] = ct
[19] = 0;
310 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
315 month
= (ct
[6] == 'n') ? 5 : 6;
316 } else if (ct
[4] == 'F') {
318 } else if (ct
[4] == 'M') {
319 month
= (ct
[6] == 'r') ? 2 : 5;
320 } else if (ct
[4] == 'A') {
321 month
= (ct
[5] == 'p') ? 3 : 7;
322 } else if (ct
[4] == 'S') {
324 } else if (ct
[4] == 'O') {
326 } else if (ct
[4] == 'N') {
330 rpc_send (sock
, RPC_INT
, atoi (&ct
[17]), /* sec */
331 RPC_INT
, atoi (&ct
[14]), /* min */
332 RPC_INT
, atoi (&ct
[11]), /* hour */
333 RPC_INT
, atoi (&ct
[8]), /* mday */
334 RPC_INT
, atoi (&ct
[20]), /* year */
335 RPC_INT
, month
, /* month */
338 long ltime
= (long) t
;
341 snprintf (buf
, sizeof (buf
), "%lx", ltime
);
342 rpc_send (sock
, RPC_STRING
, buf
, RPC_END
);
347 send_stat_info (struct stat
*st
)
351 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
357 #ifdef HAVE_STRUCT_STAT_ST_RDEV
358 mylong
= st
->st_rdev
;
362 rpc_send (msock
, RPC_INT
, (long) mylong
, RPC_INT
, (long) st
->st_ino
,
363 RPC_INT
, (long) st
->st_mode
, RPC_INT
, (long) st
->st_nlink
,
364 RPC_INT
, (long) st
->st_uid
, RPC_INT
, (long) st
->st_gid
,
365 RPC_INT
, (long) st
->st_size
, RPC_INT
, (long) blocks
,
367 send_time (msock
, st
->st_atime
);
368 send_time (msock
, st
->st_mtime
);
369 send_time (msock
, st
->st_ctime
);
379 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
380 n
= lstat (file
, &st
);
381 send_status (n
, errno
);
383 send_stat_info (&st
);
394 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
395 n
= fstat (handle
, &st
);
396 send_status (n
, errno
);
400 send_stat_info (&st
);
410 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
412 n
= stat (file
, &st
);
413 send_status (n
, errno
);
415 send_stat_info (&st
);
421 /* {{{ Directory lookup operations */
425 DIR *dirs
[OPENDIR_HANDLES
];
426 char *names
[OPENDIR_HANDLES
];
430 close_handle (int handle
)
432 if (mcfs_DIR
.used
> 0)
434 if (mcfs_DIR
.dirs
[handle
])
435 closedir (mcfs_DIR
.dirs
[handle
]);
436 g_free (mcfs_DIR
.names
[handle
]);
437 mcfs_DIR
.dirs
[handle
] = 0;
438 mcfs_DIR
.names
[handle
] = 0;
448 rpc_get (msock
, RPC_STRING
, &arg
, RPC_END
);
450 if (mcfs_DIR
.used
== OPENDIR_HANDLES
) {
451 send_status (-1, ENFILE
); /* Error */
457 for (i
= 0; i
< OPENDIR_HANDLES
; i
++) {
458 if (mcfs_DIR
.dirs
[i
] == 0) {
465 send_status (-1, EMFILE
);
469 "OOPS! you have found a bug in mc - do_opendir()!\n");
474 printf ("handle=%d\n", handle
);
477 mcfs_DIR
.dirs
[handle
] = p
;
478 mcfs_DIR
.names
[handle
] = arg
;
481 /* Because 0 is an error value */
482 rpc_send (msock
, RPC_INT
, handle
+ 1, RPC_INT
, 0, RPC_END
);
485 send_status (-1, errno
);
490 /* Sends the complete directory listing, as well as the stat information */
494 struct dirent
*dirent
;
498 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
501 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
505 /* We incremented it in opendir */
508 while ((dirent
= readdir (mcfs_DIR
.dirs
[handle
]))) {
511 int length
= NLENGTH (dirent
);
513 rpc_send (msock
, RPC_INT
, length
, RPC_END
);
514 rpc_send (msock
, RPC_BLOCK
, length
, dirent
->d_name
, RPC_END
);
516 strlen (mcfs_DIR
.names
[handle
]) + strlen (dirent
->d_name
) + 2;
517 fname
= malloc (fname_len
);
518 snprintf (fname
, fname_len
, "%s/%s", mcfs_DIR
.names
[handle
],
520 n
= lstat (fname
, &st
);
522 send_status (n
, errno
);
524 send_stat_info (&st
);
526 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
534 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
535 close_handle (handle
- 1);
540 /* {{{ Operations with one and two file name argument */
548 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
550 status
= chdir (file
);
551 send_status (status
, errno
);
561 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
563 status
= rmdir (file
);
564 send_status (status
, errno
);
574 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
576 status
= mkdir (file
, mode
);
577 send_status (status
, errno
);
585 int mode
, dev
, status
;
587 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_INT
, &dev
,
590 status
= mknod (file
, mode
, dev
);
591 send_status (status
, errno
);
602 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
603 n
= readlink (file
, buf
, 2048 - 1);
604 send_status (n
, errno
);
607 rpc_send (msock
, RPC_STRING
, buf
, RPC_END
);
618 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
619 status
= unlink (file
);
620 send_status (status
, errno
);
630 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
631 status
= rename (f1
, f2
);
632 send_status (status
, errno
);
643 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
644 status
= symlink (f1
, f2
);
645 send_status (status
, errno
);
656 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
657 status
= link (f1
, f2
);
658 send_status (status
, errno
);
666 /* {{{ Misc commands */
671 rpc_send (msock
, RPC_STRING
, (home_dir
) ? home_dir
: "/", RPC_END
);
677 rpc_send (msock
, RPC_STRING
, (up_dir
) ? up_dir
: "/", RPC_END
);
686 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
687 status
= chmod (file
, mode
);
688 send_status (status
, errno
);
696 int owner
, group
, status
;
698 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &owner
, RPC_INT
, &group
,
700 status
= chown (file
, owner
, group
);
701 send_status (status
, errno
);
714 struct utimbuf times
;
716 rpc_get (msock
, RPC_STRING
, &file
, RPC_STRING
, &as
, RPC_STRING
, &ms
,
718 sscanf (as
, "%lx", &atime
);
719 sscanf (ms
, "%lx", &mtime
);
721 printf ("Got a = %s, m = %s, comp a = %ld, m = %ld\n", as
, ms
,
725 times
.actime
= (time_t) atime
;
726 times
.modtime
= (time_t) mtime
;
727 status
= utime (file
, ×
);
728 send_status (status
, errno
);
741 const char *username
;
742 const char *password
;
746 mc_pam_conversation (int messages
, const struct pam_message
**msg
,
747 struct pam_response
**resp
, void *appdata_ptr
)
749 struct pam_response
*r
;
750 struct user_pass
*up
= appdata_ptr
;
753 r
= (struct pam_response
*) malloc (sizeof (struct pam_response
) *
759 for (status
= PAM_SUCCESS
; messages
--; msg
++, r
++) {
760 switch ((*msg
)->msg_style
) {
762 case PAM_PROMPT_ECHO_ON
:
763 r
->resp
= strdup (up
->username
);
764 r
->resp_retcode
= PAM_SUCCESS
;
767 case PAM_PROMPT_ECHO_OFF
:
768 r
->resp
= strdup (up
->password
);
769 r
->resp_retcode
= PAM_SUCCESS
;
774 r
->resp_retcode
= PAM_SUCCESS
;
779 r
->resp_retcode
= PAM_SUCCESS
;
786 static struct pam_conv conv
= { &mc_pam_conversation
, NULL
};
789 /* Return 0 if authentication failed, 1 otherwise */
791 mc_pam_auth (const char *username
, const char *password
)
797 up
.username
= username
;
798 up
.password
= password
;
799 conv
.appdata_ptr
= &up
;
802 pam_start ("mcserv", username
, &conv
, &pamh
)) != PAM_SUCCESS
)
804 if ((status
= pam_authenticate (pamh
, 0)) != PAM_SUCCESS
)
806 if ((status
= pam_acct_mgmt (pamh
, 0)) != PAM_SUCCESS
)
808 if ((status
= pam_setcred (pamh
, PAM_ESTABLISH_CRED
)) != PAM_SUCCESS
)
810 pam_end (pamh
, status
);
814 pam_end (pamh
, status
);
818 #else /* !HAVE_PAM */
820 /* Keep reading until we find a \n */
827 if (read (sock
, &c
, 1) <= 0)
835 ftp_answer (int sock
, const char *text
)
840 socket_read_block (sock
, answer
, 3);
842 if (strcmp (answer
, text
) == 0)
848 send_string (int sock
, const char *string
)
850 return socket_write_block (sock
, string
, strlen (string
));
854 do_ftp_auth (const char *username
, const char *password
)
856 struct sockaddr_in local_address
;
857 unsigned long inaddr
;
861 memset ((char *) &local_address
, 0, sizeof (local_address
));
863 local_address
.sin_family
= AF_INET
;
864 /* FIXME: extract the ftp port with the proper function */
865 local_address
.sin_port
= htons (21);
867 /* Convert localhost to usable format */
868 if ((inaddr
= inet_addr ("127.0.0.1")) != INADDR_NONE
)
869 memcpy ((char *) &local_address
.sin_addr
, (char *) &inaddr
,
872 if ((my_socket
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0) {
874 fprintf (stderr
, "do_auth: can't create socket\n");
878 (my_socket
, (struct sockaddr
*) &local_address
,
879 sizeof (local_address
)) < 0) {
881 "do_auth: can't connect to ftp daemon for authentication\n");
885 send_string (my_socket
, "user ");
886 send_string (my_socket
, username
);
887 send_string (my_socket
, "\r\n");
888 if (!ftp_answer (my_socket
, "331")) {
889 send_string (my_socket
, "quit\r\n");
893 next_line (my_socket
); /* Eat all the line */
894 send_string (my_socket
, "pass ");
895 send_string (my_socket
, password
);
896 send_string (my_socket
, "\r\n");
897 socket_read_block (my_socket
, answer
, 3);
899 send_string (my_socket
, "\r\n");
900 send_string (my_socket
, "quit\r\n");
902 if (strcmp (answer
, "230") == 0)
909 do_classic_auth (const char *username
, const char *password
)
912 const char *encr_pwd
= NULL
;
918 if ((pw
= getpwnam (username
)) == 0)
924 /* Password expiration is not checked! */
925 if ((spw
= getspnam (username
)) == NULL
)
928 encr_pwd
= spw
->sp_pwdp
;
932 encr_pwd
= pw
->pw_passwd
;
935 if (strcmp (crypt (password
, encr_pwd
), encr_pwd
) == 0)
941 #endif /* HAVE_CRYPT */
942 #endif /* !HAVE_PAM */
944 /* Try to authenticate the user based on:
945 - PAM if the system has it, else it checks:
946 - pwdauth if the system supports it.
947 - conventional auth (check salt on /etc/passwd, crypt, and compare
948 - try to contact the local ftp server and login (if -f flag used)
951 do_auth (const char *username
, const char *password
)
956 if (strcmp (username
, "anonymous") == 0)
960 if (mc_pam_auth (username
, password
) == 0)
962 #else /* if there is no pam */
964 if (pwdauth (username
, password
) == 0)
969 if (do_classic_auth (username
, password
))
974 auth
= do_ftp_auth (username
, password
);
980 this = getpwnam (username
);
984 if (chdir (this->pw_dir
) == -1)
987 if (this->pw_dir
[strlen (this->pw_dir
) - 1] == '/')
988 home_dir
= strdup (this->pw_dir
);
990 char *new_home_dir
= malloc (strlen (this->pw_dir
) + 2);
992 strcpy (new_home_dir
, this->pw_dir
);
993 strcat (new_home_dir
, "/");
994 home_dir
= new_home_dir
;
1000 if (setgid (this->pw_gid
) == -1)
1003 #ifdef HAVE_INITGROUPS
1005 if (NGROUPS_MAX
> 1 && initgroups (this->pw_name
, this->pw_gid
))
1010 #if defined (HAVE_SETUID)
1011 if (setuid (this->pw_uid
))
1013 #elif defined (HAVE_SETREUID)
1014 if (setreuid (this->pw_uid
, this->pw_uid
))
1018 /* If the setuid call failed, then deny access */
1019 /* This should fix the problem on those machines with strange setups */
1020 if (getuid () != this->pw_uid
)
1023 if (strcmp (username
, "ftp") == 0)
1024 chroot (this->pw_dir
);
1032 do_rauth (int socket
)
1034 struct sockaddr_in from
;
1037 if (getpeername (0, (struct sockaddr
*) &from
, &fromlen
) < 0)
1039 from
.sin_port
= ntohs ((unsigned short) from
.sin_port
);
1041 /* Strange, this should not happend */
1042 if (from
.sin_family
!= AF_INET
)
1045 hp
= gethostbyaddr ((char *) &fromp
.sin_addr
, sizeof (struct in_addr
),
1054 sock
= 0; /* prevent warning */
1059 login_reply (int _logged_in
)
1061 rpc_send (msock
, RPC_INT
, _logged_in
? MC_LOGINOK
: MC_INVALID_PASS
,
1065 /* FIXME: Implement the anonymous login */
1073 rpc_get (msock
, RPC_LIMITED_STRING
, &up_dir
, RPC_LIMITED_STRING
,
1074 &username
, RPC_END
);
1076 printf ("username: %s\n", username
);
1079 logged_in
= do_rauth (msock
);
1081 login_reply (logged_in
);
1085 rpc_send (msock
, RPC_INT
, MC_NEED_PASSWORD
, RPC_END
);
1086 rpc_get (msock
, RPC_INT
, &result
, RPC_END
);
1087 if (result
== MC_QUIT
)
1089 if (result
!= MC_PASS
) {
1091 printf ("do_login: Unknown response: %d\n", result
);
1094 rpc_get (msock
, RPC_LIMITED_STRING
, &password
, RPC_END
);
1095 logged_in
= do_auth (username
, password
);
1097 login_reply (logged_in
);
1102 /* {{{ Server and dispatching functions */
1104 /* This structure must be kept in synch with mcfs.h enums */
1106 static const struct _command
{
1107 const char *command
;
1108 void (*callback
) (void);
1112 "close", do_close
}, {
1114 "write", do_write
}, {
1115 "opendir", do_opendir
}, {
1116 "readdir", do_readdir
}, {
1117 "closedir", do_closedir
}, {
1118 "stat ", do_stat
}, {
1119 "lstat ", do_lstat
}, {
1120 "fstat", do_fstat
}, {
1121 "chmod", do_chmod
}, {
1122 "chown", do_chown
}, {
1123 "readlink ", do_readlink
}, {
1124 "unlink", do_unlink
}, {
1125 "rename", do_rename
}, {
1126 "chdir ", do_chdir
}, {
1127 "lseek", do_lseek
}, {
1128 "rmdir", do_rmdir
}, {
1129 "symlink", do_symlink
}, {
1130 "mknod", do_mknod
}, {
1131 "mkdir", do_mkdir
}, {
1133 "gethome", do_gethome
}, {
1134 "getupdir", do_getupdir
}, {
1135 "login", do_login
}, {
1137 "utime", do_utime
}};
1139 static int ncommands
= sizeof (commands
) / sizeof (struct _command
);
1142 exec_command (int command
)
1144 if (command
< 0 || command
>= ncommands
1145 || commands
[command
].command
== 0) {
1146 fprintf (stderr
, "Got unknown command: %d\n", command
);
1150 printf ("Command: %s\n", commands
[command
].command
);
1151 (*commands
[command
].callback
) ();
1155 check_version (void)
1159 rpc_get (msock
, RPC_INT
, &version
, RPC_END
);
1160 if (version
>= 1 && version
<= RPC_PROGVER
)
1161 rpc_send (msock
, RPC_INT
, MC_VERSION_OK
, RPC_END
);
1163 rpc_send (msock
, RPC_INT
, MC_VERSION_MISMATCH
, RPC_END
);
1165 clnt_version
= version
;
1168 /* This routine is called by rpc_get/rpc_send when the connection is closed */
1170 tcp_invalidate_socket (int sock
)
1173 printf ("Connection closed [socket %d]\n", sock
);
1187 if (rpc_get (sock
, RPC_INT
, &command
, RPC_END
)
1188 && (logged_in
|| command
== MC_LOGIN
))
1189 exec_command (command
);
1190 } while (!quit_server
);
1195 /* {{{ Net support code */
1198 get_client (int port
)
1200 int sock
, newsocket
;
1201 unsigned int clilen
;
1202 struct sockaddr_in client_address
, server_address
;
1205 if ((sock
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0)
1206 return "Cannot create socket";
1208 /* Use this to debug: */
1210 (sock
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &yes
, sizeof (yes
)) < 0)
1211 return "setsockopt failed";
1213 memset ((char *) &server_address
, 0, sizeof (server_address
));
1214 server_address
.sin_family
= AF_INET
;
1215 server_address
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
1216 server_address
.sin_port
= htons (port
);
1219 (sock
, (struct sockaddr
*) &server_address
,
1220 sizeof (server_address
)) < 0)
1221 return "Cannot bind";
1228 clilen
= sizeof (client_address
);
1230 accept (sock
, (struct sockaddr
*) &client_address
, &clilen
);
1232 if (isDaemon
&& (child
= fork ())) {
1236 waitpid (child
, &status
, 0);
1240 if (isDaemon
&& fork ())
1249 #ifdef HAVE_PMAP_SET
1251 signal_int_handler (int sig
)
1255 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1259 #ifndef IPPORT_RESERVED
1260 #define IPPORT_RESERVED 1024
1264 get_port_number (void)
1268 #ifdef HAVE_RRESVPORT
1269 int start_port
= IPPORT_RESERVED
;
1271 port
= rresvport (&start_port
);
1273 if (geteuid () == 0) {
1275 "Cannot bind the server on a reserved port\n");
1276 DO_QUIT_NONVOID (-1);
1284 port
= mcserver_port
;
1290 register_port (int port
, int abort_if_fail
)
1292 #ifdef HAVE_PMAP_SET
1293 /* Register our service with the portmapper */
1294 /* protocol: pmap_set (prognum, versnum, protocol, portp) */
1296 if (pmap_set (RPC_PROGNUM
, RPC_PROGVER
, IPPROTO_TCP
, port
))
1297 signal (SIGINT
, signal_int_handler
);
1299 fprintf (stderr
, "Cannot register service with portmapper\n");
1304 if (abort_if_fail
) {
1306 "This system lacks port registration, try using the -p\n"
1307 "flag to force installation at a given port");
1315 main (int argc
, char *argv
[])
1320 while ((c
= getopt (argc
, argv
, "fdiqp:v")) != -1) {
1340 portnum
= atoi (optarg
);
1353 "Usage is: mcserv [options] [-p portnum]\n\n"
1354 "options are:\n" "-d become a daemon (sets -q)\n"
1356 /* "-r use rhost based authentication\n" */
1358 "-f force ftp authentication\n"
1361 "-p to specify a port number to listen\n");
1367 if (isDaemon
&& fork ())
1371 portnum
= get_port_number ();
1373 if (portnum
!= -1) {
1374 register_port (portnum
, 0);
1376 printf ("Using port %d\n", portnum
);
1377 if ((result
= get_client (portnum
)))
1379 #ifdef HAVE_PMAP_SET
1381 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1387 /* FIXME: This function should not be used in mcserv */
1389 vfs_die (const char *m
)