[mw-devel] MW3 r1035 - trunk/src

arthur at sucs.org arthur at sucs.org
Fri Apr 18 14:04:05 BST 2008


Author: arthur
Date: 2008-04-18 14:04:05 +0100 (Fri, 18 Apr 2008)
New Revision: 1035

Modified:
   trunk/src/add.c
   trunk/src/del_user.c
   trunk/src/edit.c
   trunk/src/init.c
   trunk/src/js.c
   trunk/src/main.c
   trunk/src/newmain.c
   trunk/src/proto.h
   trunk/src/script.c
Log:
abstract out the seteuid calls, and fix jscript errors in the process


Modified: trunk/src/add.c
===================================================================
--- trunk/src/add.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/add.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -218,7 +218,6 @@
 			long size;
 			char *myeditor;
 			int child;
-			int myid;
 
 			if (u_reg(user->status))
 			{
@@ -227,8 +226,7 @@
 			}else
 				myeditor=SECUREEDITOR;
 
-			myid=geteuid();
-			if (seteuid(getuid())==-1) 
+			if (perms_drop()==-1) 
 			{
 				printf(_("Failed to set user id, aborting.\n"));
 				perror("setuid");
@@ -251,7 +249,7 @@
 			if (tfile < 0)
 			{	
 				printf(_("Cannot create temporary file: %s\n"), strerror(errno));
-				seteuid(myid);
+				perms_restore();
 				free(fol);
 				free(new);
 				free(buff);
@@ -301,7 +299,7 @@
 			new->size=size;
 			close(tfile);
 			unlink(fullpath);
-			seteuid(myid);
+			perms_restore();
 		}
 	}while (!stringcmp(answer,_("post"),1));
 

Modified: trunk/src/del_user.c
===================================================================
--- trunk/src/del_user.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/del_user.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <fcntl.h>
 #include "bb.h"
+#include "files.h"
+
 int internet=0;
 struct person user; 
 

Modified: trunk/src/edit.c
===================================================================
--- trunk/src/edit.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/edit.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -740,10 +740,8 @@
 		long size;
 		int child;
 		char *myeditor;
-		int myid;
 
-		myid=geteuid();
-		if (seteuid(getuid())==-1) 
+		if (perms_drop()==-1) 
 		{
 			printf(_("Failed to set user id, aborting.\n"));
 			perror("setuid");
@@ -756,7 +754,7 @@
 		if (tfile < 0)
 		{
 			perror("creating temp file");
-			seteuid(myid);
+			perms_restore();
 			return;
 		}
 		write(tfile,buff,head.size);
@@ -795,7 +793,7 @@
 		if ((tfile=open(fullpath,O_RDONLY))<0)
 		{
 			perror("reading temp file");
-			seteuid(myid);
+			perms_restore();
 			return;
 		}
 		size=lseek(tfile,0,2);
@@ -804,7 +802,7 @@
 		read(tfile,buff,size);
 		head.size=size;
 		close(tfile);
-		seteuid(myid);
+		perms_restore();
 		/* end of setuid stuff */
 
 		Lock_File(text);

Modified: trunk/src/init.c
===================================================================
--- trunk/src/init.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/init.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -36,13 +36,9 @@
 void LoadInitFile(char *name)
 {
 	struct passwd *pw;
-	int myid;
 
 	if ((pw=getpwuid(getuid()))!=NULL)
 	{
-		myid=geteuid();
-		seteuid(getuid());
-
 		/* try to load the personal copy*/
 		if (ReadInitFile(pw->pw_dir, name)) {
 			/* try the system wide one instead */
@@ -51,8 +47,6 @@
 					fprintf(stderr, "Could not find file %s to load it.\n", name);
 			}
 		}
-
-		seteuid(myid);
 	}
 }
 
@@ -70,6 +64,7 @@
 		CURL *cl;
 		char cerr[CURL_ERROR_SIZE];
 		/* use libcurl to grab the file */
+		perms_drop();
 		file = tmpfile();
 		if (file == NULL) {
 			fprintf(stderr, "Error opening temporary file\n");
@@ -90,6 +85,7 @@
 		if (curl_easy_perform(cl))
 			fprintf(stderr, "Error loading %s: %s\n", filename, cerr);
 		curl_easy_cleanup(cl);
+		perms_restore();
 		fseek(file, 0, SEEK_SET);
 	} else {
 		if (filename[0] == '/' ||
@@ -100,6 +96,7 @@
 			return 1;
 		}
 		snprintf(path, 1023, "%s/%s", base, filename);
+		perms_drop();
 		if (stat(path, &stats))
 		{
 			/* be quiet about its not there, handle higher up */
@@ -116,6 +113,7 @@
 			if (strcmp(".mwrc", filename))	printf(_("Error reading %s: %s\n"), path, strerror(errno));
 			return 1;
 		}
+		perms_restore();
 	}
 
 	if ((a=strrchr(filename, '.'))!=NULL && strncasecmp(a, ".js", 3)==0) {
@@ -402,3 +400,21 @@
 	fclose(file);
 	return 0;
 }
+
+/* drop and restore user level privs */
+static int private_myid = -1;
+
+int perms_drop(void)
+{
+	private_myid=geteuid();
+	if (seteuid(getuid()) == -1) return -1;
+	return private_myid;
+}
+
+void perms_restore(void)
+{
+	if (private_myid != -1) {
+		seteuid(private_myid);
+		private_myid = -1;
+	}
+}

Modified: trunk/src/js.c
===================================================================
--- trunk/src/js.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/js.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -984,7 +984,7 @@
 								 // if this becomes a problem dynamic allocation of the utf8 buffer might be needed although getting the right size is a pita
 	//JSObject *result; // result object were creating
 	jsval resobject_jsval;
-	int myid, conversion_result;
+	int conversion_result;
 	char path[1024];
 	struct passwd *pw;
 	JSBool retval;
@@ -1025,10 +1025,9 @@
 	}
 	snprintf(path, sizeof(path), "%s/%s", pw->pw_dir, dbname);
 
-	myid=geteuid();
-	seteuid(getuid());
+	perms_drop();
 	dbres = js_db_query(path, query);
-	seteuid(myid);
+	perms_restore();
 
 	if (!dbres) {
 		JS_ReportError(cx, "Major error in javascript database query.");

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/main.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -1774,12 +1774,11 @@
 	    matches && matches[0] && !matches[1])
 	{
 		char *match = matches[0];
-		uid_t uid = geteuid();
 		struct stat stats;
 		char *path = strdup(match);
 
 		(void)(*rl_directory_rewrite_hook)(&path);
-		seteuid(getuid());
+		perms_drop();
 		if (stat(path, &stats) == 0 &&
 		    S_ISDIR(stats.st_mode))
 		{
@@ -1788,7 +1787,7 @@
 			else
 				rl_completion_append_character = '/';
 		}
-		seteuid(uid);
+		perms_restore();
 		free(path);
 	}
 	return(matches);

Modified: trunk/src/newmain.c
===================================================================
--- trunk/src/newmain.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/newmain.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -829,9 +829,7 @@
 	if (x==0)
 	{
 		/* we are child */
-		int uid;
-		uid=geteuid();
-		if (seteuid(getuid())==-1) {perror("setuid");exit(0);}
+		if (perms_drop()==-1) {perror("setuid");exit(0);}
 		if ((f=fopen(buff,"a"))==NULL)
 		{
 			perror(file);
@@ -839,7 +837,7 @@
 		}
 		output=f;
 		fprintf(output,"Saved Message:\n");
-		seteuid(uid);	
+		perms_restore();
 		read_msg(currentfolder,msg, user);
 		fclose(output);
 		printf("Mesg no %d saved to %s\n",msg,buff);

Modified: trunk/src/proto.h
===================================================================
--- trunk/src/proto.h	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/proto.h	2008-04-18 13:04:05 UTC (rev 1035)
@@ -178,6 +178,8 @@
 /*void help_list(CommandList *c);*/
 /* init.c */
 void LoadInitFile(char *);
+int perms_drop(void);
+void perms_restore(void);
 /* talker_privs.c */
 unsigned long cm_flags(unsigned long cm, unsigned long flags, int mode);
 char *display_cmflags(unsigned long cm);

Modified: trunk/src/script.c
===================================================================
--- trunk/src/script.c	2008-04-13 00:21:44 UTC (rev 1034)
+++ trunk/src/script.c	2008-04-18 13:04:05 UTC (rev 1035)
@@ -1135,7 +1135,6 @@
     char	       *trimmed_name;
     struct stat		stats;
     int			isdir;
-    uid_t		uid;
     struct passwd      *pw;
     char	       *part_path;
 
@@ -1158,8 +1157,7 @@
     {
 	/* Set euid to real uid so completion can't be used to read Arthur's
 	 * home directory */
-	uid = geteuid();
-	seteuid(getuid());
+	perms_drop();
 	/* Keep getting filenames until we see a legal one */
 	do
 	{
@@ -1198,7 +1196,7 @@
 		break;
 	    }
 	} while (!relname);
-	seteuid(uid);
+	perms_restore();
     }
 
     return relname;





More information about the mw-devel mailing list