# Add the ability to choose alternate RPC ports at runtime and disable
# security so that it can run as a userland process
# Patch origin: Wind River

Index: nfs-server-2.2beta47/auth_init.c
===================================================================
--- nfs-server-2.2beta47.orig/auth_init.c
+++ nfs-server-2.2beta47/auth_init.c
@@ -409,6 +409,7 @@ auth_init(char *fname)
 		fname = EXPORTSFILE;
 	auth_file = fname;	/* Save for re-initialization */
 
+#ifdef ROOT_LEVEL_SECURITY
 	/* Check protection of exports file. */
 	switch(iCheckAccess(auth_file, EXPORTSOWNERUID, EXPORTSOWNERGID)) {
 	case FACCESSWRITABLE:
@@ -424,6 +425,7 @@ auth_init(char *fname)
 		Dprintf(L_ERROR, "exiting because of security violation.\n");
 		exit(1);
 	}
+#endif
 
 	if ((ef = fopen(fname, "r")) == NULL) {
 		Dprintf(L_ERROR, "Could not open exports file %s: %s\n",
Index: nfs-server-2.2beta47/nfsd.c
===================================================================
--- nfs-server-2.2beta47.orig/nfsd.c
+++ nfs-server-2.2beta47/nfsd.c
@@ -46,6 +46,7 @@ static char pathbuf_1[NFS_MAXPATHLEN + N
 
 extern char version[];
 static char *program_name;
+static int nfs_prog = NFS_PROGRAM;
 
 /*
  * Option table
@@ -60,6 +61,7 @@ static struct option longopts[] = {
       { "help",			0,			0,	'h' },
       { "log-transfers",	0,			0,	'l' },
       { "allow-non-root",	0,			0,	'n' },
+      { "prog",			required_argument,	0,	'g' },
       { "port",			required_argument,	0,	'P' },
       { "promiscuous",		0,			0,	'p' },
       { "re-export",		0,			0,	'r' },
@@ -73,9 +75,10 @@ static struct option longopts[] = {
       { "udp-only",		0,			0,	OPT_NOTCP },
       {	"loopback-only",	0,			0,	OPT_LOOPBACK },
       { "hashed-inodes",	0,			0,	'I' },
+      { "nfs-pid",	required_argument,			0,	'N' },
       { NULL,		0,	0, 0 }
 };
-static const char *	shortopts = "a:d:Ff:hlnP:prR:tvz::";
+static const char *	shortopts = "a:d:Ff:g:hlnN:P:prR:tvz::";
 
 /*
  * Table of supported versions
@@ -1003,6 +1006,8 @@ main(int argc, char **argv)
 	int	failsafe_level = 0;
 	int	c;
 	int	i, ncopies = 1;
+    char *nfs_pid_file = NULL;
+
 
 	program_name = argv[0];
 	chdir("/");
@@ -1026,9 +1031,15 @@ main(int argc, char **argv)
 		case 'f':
 			auth_file = optarg;
 			break;
+		case 'g':
+			nfs_prog = atoi(optarg);
+			break;
 		case 'l':
 			log_transfers = 1;
 			break;
+        case 'N':
+            nfs_pid_file = strdup(optarg);
+            break;
 		case 'n':
 			allow_non_root = 1;
 			break;
@@ -1114,7 +1125,7 @@ main(int argc, char **argv)
 	log_open("nfsd", foreground);
 
 	/* Initialize RPC stuff */
-	rpc_init("nfsd", NFS_PROGRAM, nfsd_versions, nfs_dispatch,
+	rpc_init("nfsd", nfs_prog, nfsd_versions, nfs_dispatch,
 				nfsport, NFS_MAXDATA);
 
 	if (_rpcpmstart) {
@@ -1145,7 +1156,10 @@ main(int argc, char **argv)
 	/* Initialize the AUTH module. */
 	auth_init(auth_file);
 
-	setpidpath(_PATH_NFSD_PIDFILE);
+    if (nfs_pid_file == 0)
+        nfs_pid_file = _PATH_NFSD_PIDFILE;
+    setpidpath(nfs_pid_file);
+
 	if (failsafe_level == 0) {
 		/* Start multiple copies of the server */
 		writepid(getpid(), 1);
@@ -1215,9 +1229,11 @@ usage(FILE *fp, int n)
 	fprintf(fp,
 "Usage: %s [-Fhnpv] [-d kind] [-f exports-file] [-P port] [--version]\n"
 "       [--debug kind] [--exports-file=file] [--port port]\n"
+"       [--prog alternate_rpc_port_nubmer]\n"
 "       [--allow-non-root] [--promiscuous] [--version] [--foreground]\n"
 "       [--re-export] [--log-transfers] [--public-root path]\n"
 "       [--no-spoof-trace] [--no-cross-mounts] [--hashed-inodes] [--help]\n"
+"       [--nfs-pid file]\n"
 						, program_name);
 	exit(n);
 }
@@ -1234,7 +1250,7 @@ sigterm(int sig)
 static void
 terminate(void)
 {
-	rpc_exit(NFS_PROGRAM, nfsd_versions);
+	rpc_exit(nfs_prog, nfsd_versions);
 	efs_shutdown();
 }
 
Index: nfs-server-2.2beta47/mountd.c
===================================================================
--- nfs-server-2.2beta47.orig/mountd.c
+++ nfs-server-2.2beta47/mountd.c
@@ -42,6 +42,7 @@ int hashed_inodes; /* dummy */
 static void	usage(FILE *, int);
 static void	terminate(void);
 static RETSIGTYPE sigterm(int sig);
+int mount_prog = MOUNTPROG;
 
 /*
  * Option table for mountd
@@ -55,6 +56,7 @@ static struct option longopts[] =
       { "help",			0,			0,	'h' },
       { "allow-non-root",	0,			0,	'n' },
       { "port",			required_argument,	0,	'P' },
+      { "prog",			required_argument,	0,	'g' },
       { "promiscous",		0,			0,	'p' },
       { "re-export",		0,			0,	'r' },
       {	"no-spoof-trace",	0,			0,	't' },
@@ -63,9 +65,11 @@ static struct option longopts[] =
       { "no-cross-mounts",	0,			0,	'x' },
       { "no-tcp",		0,			0,	OPT_NOTCP },
       {	"loopback-only",	0,			0,	OPT_LOOPBACK },
+      { "mount-pid",	required_argument,			0,	'N' },
+      { "rmtab",	required_argument,			0,	'R' },
       { NULL,			0,			0,	0 }
 };
-static const char *	shortopts = "Fd:f:hnpP:rtvz::";
+static const char *	shortopts = "Fd:f:g:hnN:pP:rRtvz::";
 
 /*
  * Table of supported versions
@@ -318,6 +322,7 @@ main(int argc, char **argv)
 	int failsafe_level = 0;
 	int port = 0;
 	int c;
+    char *mount_pid_file = NULL;
 
 	program_name = argv[0];
 
@@ -340,9 +345,15 @@ main(int argc, char **argv)
 		case 'f':
 			auth_file = optarg;
 			break;
+		case 'g':
+			mount_prog = port = atoi(optarg);
+			break;
 		case 'n':
 			allow_non_root = 1;
 			break;
+        case 'N':
+            mount_pid_file = strdup(optarg);
+            break;
 		case 'P':
 			port = atoi(optarg);
 			if (port <= 0 || port > 65535) {
@@ -354,6 +365,9 @@ main(int argc, char **argv)
 		case 'p':
 			promiscuous = 1;
 			break;
+        case 'R':
+            _PATH_RMTAB = strdup(optarg);
+            break;
 		case 'r':
 			re_export = 1;
 			break;
@@ -401,7 +415,7 @@ main(int argc, char **argv)
 	log_open("mountd", foreground);
 
 	/* Create services and register with portmapper */
-	rpc_init("mountd", MOUNTPROG, mountd_versions, mount_dispatch, port, 0);
+	rpc_init("mountd", mount_prog, mountd_versions, mount_dispatch, port, 0);
 
 	if (_rpcpmstart) {
 		/* Always foreground mode */
@@ -422,7 +436,9 @@ main(int argc, char **argv)
 	auth_init(auth_file);
 
 	/* Write pidfile */
-	setpidpath(_PATH_MOUNTD_PIDFILE);
+    if (mount_pid_file == 0)
+        mount_pid_file = _PATH_MOUNTD_PIDFILE;
+    setpidpath(mount_pid_file);
 	writepid(getpid(), 1);
 
 	/* Failsafe mode */
@@ -453,7 +469,9 @@ usage(FILE *fp, int n)
 				program_name);
 	fprintf(fp, "       [--debug kind] [--help] [--allow-non-root]\n");
 	fprintf(fp, "       [--promiscuous] [--version] [--port portnum]\n");
+	fprintf(fp, "       [--prog alternate_rpc_port_nubmer]\n");
 	fprintf(fp, "       [--exports-file=file] [--no-cross-mounts]\n");
+    fprintf(fp, "       [--mount-pid file] [--rmtab file]\n");
 	exit(n);
 }
 
@@ -467,7 +485,7 @@ sigterm(int sig)
 static void
 terminate(void)
 {
-	rpc_exit(MOUNTPROG, mountd_versions);
+	rpc_exit(mount_prog, mountd_versions);
 }
 
 RETSIGTYPE
Index: nfs-server-2.2beta47/rmtab.c
===================================================================
--- nfs-server-2.2beta47.orig/rmtab.c
+++ nfs-server-2.2beta47/rmtab.c
@@ -14,6 +14,8 @@ static char *	rmtab_gethost(struct svc_r
 static int	rmtab_insert(char *, char *);
 static void	rmtab_file(char);
 
+char *_PATH_RMTAB = _PATH_RMTAB_VAL;
+
 /*
  * global top to linklist
  */
Index: nfs-server-2.2beta47/rmtab.h
===================================================================
--- nfs-server-2.2beta47.orig/rmtab.h
+++ nfs-server-2.2beta47/rmtab.h
@@ -11,8 +11,9 @@
  * Location of rmtab file. /etc/rmtab is the standard on most systems.
  */
 #include <paths.h>
-#ifndef _PATH_RMTAB
-#define _PATH_RMTAB	"/etc/rmtab"
+extern char *_PATH_RMTAB;
+#ifndef _PATH_RMTAB_VAL
+#define _PATH_RMTAB_VAL	"/etc/rmtab"
 #endif
 
 extern void 		rmtab_add_client(dirpath, struct svc_req *);
