changeset 120:d6b7c3fb37ea

srogue: add and use more md_* portable functions. Privileges and memory usage checks are now more portable.
author John "Elwin" Edwards
date Fri, 02 May 2014 15:06:23 -0700
parents 458df24e973d
children e6c8652473fe
files srogue/daemon.c srogue/list.c srogue/main.c srogue/mdport.c srogue/rogue.h srogue/save.c
diffstat 6 files changed, 74 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/srogue/daemon.c	Wed Apr 30 14:46:30 2014 -0700
+++ b/srogue/daemon.c	Fri May 02 15:06:23 2014 -0700
@@ -173,5 +173,5 @@
 activity()
 {
 	msg("Daemons = %d : Memory Items = %d : Memory Used = %d",
-	    demoncnt,total,sbrk(0));
+	    demoncnt,total,md_memused());
 }
--- a/srogue/list.c	Wed Apr 30 14:46:30 2014 -0700
+++ b/srogue/list.c	Fri May 02 15:06:23 2014 -0700
@@ -102,7 +102,7 @@
 	register char *space = ALLOC(size);
 
 	if (space == NULL) {
-		sprintf(prbuf,"Rogue ran out of memory (%d).",sbrk(0));
+		sprintf(prbuf,"Rogue ran out of memory (%d).", md_memused());
 		fatal(prbuf);
 	}
 	total++;
--- a/srogue/main.c	Wed Apr 30 14:46:30 2014 -0700
+++ b/srogue/main.c	Fri May 02 15:06:23 2014 -0700
@@ -29,8 +29,6 @@
 #endif
 
 #ifdef BSD
-#define srand48(seed)	srandom(seed)
-#define lrand48()	random()
 #include <sys/time.h>
 #endif
 
@@ -57,13 +55,8 @@
 	if (homedir == NULL)
         	homedir = "";
 
-	playuid = getuid();
-
-	if (setuid(playuid) < 0) {
-		printf("Cannot change to effective uid: %d\n", playuid);
-		exit(1);
-	}
-	playgid = getgid();
+	playuid = md_getuid();
+	playgid = md_getgid();
 
 	/* check for print-score option */
 #ifdef SCOREFILE
@@ -121,6 +114,9 @@
 	}
 #endif
 
+	if (!use_savedir)
+		md_droppriv();
+
 	/* get home and options from environment */
 
 	if ((env = getenv("HOME")) != NULL)
@@ -177,7 +173,7 @@
 
 	fflush(stdout);
 	seed = dnum;
-	srand48(seed);			/* init rnd number gen */
+	srandom(seed);			/* init rnd number gen */
 
 	md_onsignal_exit();		/* just in case */
 
@@ -358,7 +354,7 @@
 	if (range == 0)
 		wh = 0;
 	else {
-		wh = lrand48() % range;
+		wh = random() % range;
 		wh &= 0x7FFFFFFF;
 	}
 	return wh;
--- a/srogue/mdport.c	Wed Apr 30 14:46:30 2014 -0700
+++ b/srogue/mdport.c	Fri May 02 15:06:23 2014 -0700
@@ -413,6 +413,16 @@
 #endif
 }
 
+gid_t
+md_getgid(void)
+{
+#ifdef HAVE_GETGID
+    return( getgid() );
+#else
+    return(42);
+#endif
+}
+
 pid_t
 md_getpid(void)
 {
@@ -1509,3 +1519,51 @@
 
 #endif
 
+long
+md_memused(void)
+{
+#ifdef _WIN32
+    MEMORYSTATUS stat;
+
+    GlobalMemoryStatus(&stat);
+
+    return((long)stat.dwTotalPageFile);
+#else
+    return( (long)sbrk(0) );
+#endif
+}
+
+void
+md_droppriv(void)
+{
+#if defined(HAVE_GETUID)
+	uid_t realuid = getuid();
+
+#if defined(HAVE_SETRESUID)
+    if (setresuid(-1, realuid, realuid) != 0) {
+#elif defined (HAVE_SETREUID) 
+    if (setreuid(realuid, realuid) != 0) {
+#elif defined (HAVE_SETUID)
+    if (setuid(realuid) != 0) {
+#else
+    if (0) {
+#endif
+	printf("Cannot change to effective uid: %d\n", realuid);
+	exit(1);
+    }
+#endif
+}
+
+extern uid_t playuid;
+extern gid_t playgid;
+
+void
+md_resetpriv(void)
+{
+#if defined (HAVE_SETUID)
+	setuid(playuid);
+#endif
+#if defined (HAVE_SETGID)
+	setgid(playgid);
+#endif
+}
--- a/srogue/rogue.h	Wed Apr 30 14:46:30 2014 -0700
+++ b/srogue/rogue.h	Fri May 02 15:06:23 2014 -0700
@@ -43,6 +43,7 @@
 char	*md_gethomedir(void);
 char	*md_getusername(void);
 uid_t	md_getuid(void);
+gid_t	md_getgid(void);
 char	*md_getpass(char *prompt);
 pid_t	md_getpid(void);
 char	*md_getrealname(uid_t uid);
@@ -67,6 +68,9 @@
 void md_onsignal_exit(void);
 void md_onsignal_default(void);
 int md_issymlink(char *sp);
+long md_memused(void);
+void md_droppriv(void);
+void md_resetpriv(void);
 char *xcrypt(const char *key, const char *setting);
 
 #define reg     register        /* register abbr.       */
--- a/srogue/save.c	Wed Apr 30 14:46:30 2014 -0700
+++ b/srogue/save.c	Fri May 02 15:06:23 2014 -0700
@@ -26,10 +26,6 @@
 #include "rogue.h"
 #include "rogue.ext"
 
-#ifdef BSD
-#define srand48(seed)	srandom(seed)
-#endif
-
 EXTCHAR version[];
 EXTCHAR *ctime();
 
@@ -144,8 +140,7 @@
 	ignore();
 	if (!use_savedir)
 	{
-		setuid(playuid);
-		setgid(playgid);
+		md_resetpriv();
 	}
 	umask(022);
 
@@ -302,8 +297,7 @@
 				/* set id to unlink file */
 				if(pid == 0)
 				{
-					setuid(playuid);
-					setgid(playgid);
+					md_resetpriv();
 					unlink(file);
 					exit(0);
 				}
@@ -350,6 +344,6 @@
 	strcpy(file_name, file);
 	setup();
 	restscr(cw);
-	srand48(getpid());
+	srandom(getpid());
 	playit();
 }