diff mcabber/src/utils.c @ 362:d8f147d6e872

Check directory and config file permissions * Check history directory and configuration file permissions, and correct them to 0700 and 0600 if necessary. * Warn when mcabber main directory ($HOME/.mcabber) has bad permissions * Reset UseFileLogging & FileLoadLogs when the log dir does not exist
author Mikael Berthe <mikael@lilotux.net>
date Sun, 24 Jul 2005 14:37:27 +0100
parents f562b9af2de7
children 33b8e801ffa6
line wrap: on
line diff
--- a/mcabber/src/utils.c	Sat Jul 23 21:50:06 2005 +0100
+++ b/mcabber/src/utils.c	Sun Jul 24 14:37:27 2005 +0100
@@ -26,8 +26,12 @@
 #include <string.h>
 #include <stdarg.h>
 #include <time.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include <config.h>
+#include "screen.h"
 
 static int DebugEnabled;
 static char *FName;
@@ -90,6 +94,45 @@
   }
 }
 
+//  checkset_perm(name, setmode)
+// Check the permissions of the "name" file/dir
+// If setmode is true, correct the permissions if they are wrong
+// Return values: -1 == bad file/dir, 0 == success, 1 == cannot correct
+int checkset_perm(const char *name, unsigned int setmode)
+{
+  int fd;
+  struct stat buf;
+
+  fd = lstat(name, &buf);
+  if (fd == -1) return -1;
+
+  if (buf.st_uid != geteuid()) {
+    scr_LogPrint("Wrong file owner [%s]", name);
+    return 1;
+  }
+
+  if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP) ||
+      buf.st_mode & (S_IROTH | S_IWOTH | S_IXOTH)) {
+    if (setmode) {
+      mode_t newmode = 0;
+      scr_LogPrint("Bad permissions [%s]", name);
+      if (S_ISDIR(buf.st_mode))
+        newmode |= S_IXUSR;
+      newmode |= S_IRUSR | S_IWUSR;
+      if (chmod(name, newmode)) {
+        scr_LogPrint("WARNING: Failed to correct permissions!");
+        return 1;
+      }
+      scr_LogPrint("Permissions have been corrected");
+    } else {
+      scr_LogPrint("WARNING: Bad permissions [%s]", name);
+      return 1;
+    }
+  }
+
+  return 0;
+}
+
 //  to_iso8601(dststr, timestamp)
 // Convert timestamp to iso8601 format, and store it in dststr.
 // NOTE: dststr should be at last 19 chars long.
@@ -185,4 +228,3 @@
 
   return retval;
 }
-