[mw-devel] [Git][milliways/mw][master] Fix the logic that prevented recursive init files

Alice Mitchell alice at sucs.org
Tue May 7 20:34:59 BST 2019


Alice Mitchell pushed to branch master at milliways / mw


Commits:
809488b2 by Alice Mitchell at 2019-05-07T19:30:33Z
Fix the logic that prevented recursive init files

This also fixes the use of the central script loading directory,
any load or include instruction also checks in HOMEPATH/scripts/

- - - - -


3 changed files:

- src/client/init.c
- src/client/init.h
- src/client/talker.c


Changes:

=====================================
src/client/init.c
=====================================
@@ -49,15 +49,9 @@ void perms_restore(void)
 	}
 }
 
-int mwrc_loaded = 0;
-static int ReadInitFile(const char *base, const char *filename)
+static FILE *OpenInitFile(const char *base, const char *filename)
 {
 	FILE *file;
-	char *buff, *backup, *header;
-	char path[1024];
-	char *a, *b, *c;
-	int lineno;
-	struct stat stats;
 
 	if (strncasecmp(filename, "http://", 7)==0
 	||  strncasecmp(filename, "https://", 8)==0) {
@@ -68,6 +62,7 @@ static int ReadInitFile(const char *base, const char *filename)
 		file = tmpfile();
 		if (file == NULL) {
 			fprintf(stderr, "Error opening temporary file\n");
+			return NULL;
 		}
 		cl = curl_easy_init();
 /*		curl_easy_setopt(cl, CURLOPT_NOPROGRESS, 0); */
@@ -75,18 +70,24 @@ static int ReadInitFile(const char *base, const char *filename)
 		curl_easy_setopt(cl, CURLOPT_URL, filename);
 		curl_easy_setopt(cl, CURLOPT_ERRORBUFFER, cerr);
 		curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VERSION);
-		if (curl_easy_perform(cl))
+		if (curl_easy_perform(cl)) {
 			fprintf(stderr, "Error loading %s: %s\n", filename, cerr);
+			fclose(file);
+			return NULL;
+		}
 		curl_easy_cleanup(cl);
 		perms_restore();
 		fseek(file, 0, SEEK_SET);
 	} else {
+		char path[1024];
+		struct stat stats;
+
 		if (filename[0] == '/' ||
 		    !strncmp(filename, "../", 3) ||
 		    strstr(filename, "/../"))
 		{
 			printf(_("Cannot load \"%s\": Illegal path\n"), filename);
-			return 1;
+			return NULL;
 		}
 		snprintf(path, 1023, "%s/%s", base, filename);
 		perms_drop();
@@ -94,37 +95,34 @@ static int ReadInitFile(const char *base, const char *filename)
 		{
 			/* be quiet about its not there, handle higher up */
 			perms_restore();
-			return 1;
+			return NULL;
 		}
 		if (!S_ISREG(stats.st_mode))
 		{
 			printf(_("Error reading %s: Not a regular file\n"), path);
 			perms_restore();
-			return 1;
+			return NULL;
 		}
 
 		if ((file=fopen(path,"r"))==NULL)
 		{
 			if (strcmp(".mwrc", filename))	printf(_("Error reading %s: %s\n"), path, strerror(errno));
 			perms_restore();
-			return 1;
+			return NULL;
 		}
 		perms_restore();
 	}
+	return file;
+}
 
-	if ((a=strrchr(filename, '.'))!=NULL && strncasecmp(a, ".js", 3)==0) {
-		load_jsfile(file, filename);
-		fclose(file);
-		return 0; // changed because if an error occured after this point the file exists, the js code has reported the error to the user and returning 1 will report to them that it doesn't exist
-	}
-
-	if (mwrc_loaded) {
-		fprintf(stderr, "Warning: file included in .mwrc not supported: %s\n", filename);
-		return 0;
-	} else
-		mwrc_loaded = 1;
+static int ReadInitFile(const char *base, const char *filename);
 
-	lineno=0;
+static int parse_mwrcfile(FILE *file, const char *base, const char *filename)
+{
+	char *buff, *backup, *header;
+	char *a, *b, *c;
+	int lineno = 0;
+	
 	while (!feof(file))
 	{
 		if ((buff = frl_line_nspace(file, "#")) == NULL) break;
@@ -192,11 +190,42 @@ static int ReadInitFile(const char *base, const char *filename)
 	return 0;
 }
 
-void LoadInitFile(const char *name)
+static int ReadInitFile(const char *base, const char *filename)
+{
+	FILE *file = NULL;
+	const char *a;
+
+	/* look for file in path we were given first */
+	file = OpenInitFile(base, filename);
+
+	/* it wsnt there, do we have one in the default collection */
+	if (!file) 	
+		file = OpenInitFile(HOMEPATH"/scripts", filename);
+
+	/* not found anywhere, give up */
+	if (!file)
+		return 1;
+
+
+	if ((a=strrchr(filename, '.'))!=NULL && strncasecmp(a, ".js", 3)==0) {
+		load_jsfile(file, filename);
+		fclose(file);
+		return 0; // changed because if an error occured after this point the file exists, the js code has reported the error to the user and returning 1 will report to them that it doesn't exist
+	} else {
+		if (parse_mwrcfile(file, base, filename)) {
+			fprintf(stderr, "Warning: file included in .mwrc not supported: %s\n", filename);
+			return 1;
+		}
+	}
+	return 0;
+}
+
+int LoadInitFile(const char *name)
 {
 	char *mwrc = NULL;
 	struct passwd *pw;
 	int dofree=0;
+	int ret = 1;
 
 	if (name == NULL) {
 		mwrc = userdb_get(USERDB_PRIVATE, user->record.name, "mwrc");
@@ -213,18 +242,13 @@ void LoadInitFile(const char *name)
 		fprintf(stderr, _("Failed to get user data\n"));
 		goto out;
 	}
-	/* try to load the personal copy*/
-	if (!ReadInitFile(pw->pw_dir, name)) {
-		goto out;
-	}
 
-	/* try the system wide one instead */
-	if (ReadInitFile(HOMEPATH"/scripts", name)) {
-		if (strcmp(".mwrc", name)!=0)
-			fprintf(stderr, _("Could not find init file %s\n"), name);
+	if (!ReadInitFile(pw->pw_dir, name)) {
+		ret = 0;
 	}
 out:
 	if (dofree) free(mwrc);
+	return ret;
 }
 
 


=====================================
src/client/init.h
=====================================
@@ -3,6 +3,6 @@
 
 int perms_drop(void);
 void perms_restore(void);
-void LoadInitFile(const char *name);
+int LoadInitFile(const char *name);
 
 #endif /* INIT_H */


=====================================
src/client/talker.c
=====================================
@@ -351,7 +351,8 @@ void t_load(CommandList *cm, int argc, const char **argv, char *args)
 		return;
 	}
 
-	LoadInitFile(argv[1]);
+	if (LoadInitFile(argv[1])) 
+		fprintf(stderr, "Error loading initfile %s\n", argv[1]);
 
 	/* run board init functions */
 	RunInitFuncs(0);
@@ -388,7 +389,6 @@ void t_mwrc(CommandList *cm, int argc, const char **argv, char *args)
 	}
 }
 
-extern int mwrc_loaded;
 void t_restart(CommandList *cm, int argc, const char **argv, char *args)
 {
 	DestroyAllLinks(&bind_list); bind_list = NULL;
@@ -405,7 +405,6 @@ void t_restart(CommandList *cm, int argc, const char **argv, char *args)
 
 	RoomDestroy(&user->room);
 
-	mwrc_loaded = 0;
 	RoomInit(&user->room);
 	LoadRoom(&user->room, user->record.room);
 



View it on GitLab: https://projects.sucs.org/milliways/mw/commit/809488b203c6a81a8f89ac25ddc442690e50db90

-- 
View it on GitLab: https://projects.sucs.org/milliways/mw/commit/809488b203c6a81a8f89ac25ddc442690e50db90
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20190507/ec2f28d9/attachment-0001.html>


More information about the mw-devel mailing list