<<< Date Index >>>     <<< Thread Index >>>

[PATCH] Add $umask for mailboxes and attachments



# HG changeset patch
# User Brendan Cully <brendan@xxxxxxxxxx>
# Date 1172898753 28800
# Node ID d61d41715015d975d5643a41cd8dc36b8a7dc97b
# Parent  c36482e98b8130395c5697c21d060690b57d58e4
Add $umask for mailboxes and attachments

diff -r c36482e98b81 -r d61d41715015 attach.c
--- a/attach.c  Fri Mar 02 20:24:25 2007 -0800
+++ b/attach.c  Fri Mar 02 21:12:33 2007 -0800
@@ -736,12 +736,21 @@ static FILE *
 static FILE *
 mutt_save_attachment_open (char *path, int flags)
 {
+  mode_t omask;
+  FILE *fp = NULL;
+
+  omask = umask(Umask);
+
   if (flags == M_SAVE_APPEND)
-    return fopen (path, "a");
-  if (flags == M_SAVE_OVERWRITE)
-    return fopen (path, "w");          /* __FOPEN_CHECKED__ */
-  
-  return safe_fopen (path, "w");
+    fp = fopen (path, "a");
+  else if (flags == M_SAVE_OVERWRITE)
+    fp = fopen (path, "w");            /* __FOPEN_CHECKED__ */
+  else
+    fp = safe_fopen (path, "w");
+
+  umask(omask);
+
+  return fp;
 }
 
 /* returns 0 on success, -1 on error */
diff -r c36482e98b81 -r d61d41715015 globals.h
--- a/globals.h Fri Mar 02 20:24:25 2007 -0800
+++ b/globals.h Fri Mar 02 21:12:33 2007 -0800
@@ -201,6 +201,7 @@ WHERE short SendmailWait;
 WHERE short SendmailWait;
 WHERE short SleepTime INITVAL (1);
 WHERE short Timeout;
+WHERE short Umask;
 WHERE short WrapMargin;
 WHERE short WriteInc;
 
diff -r c36482e98b81 -r d61d41715015 init.h
--- a/init.h    Fri Mar 02 20:24:25 2007 -0800
+++ b/init.h    Fri Mar 02 21:12:33 2007 -0800
@@ -2914,6 +2914,12 @@ struct option_t MuttVars[] = {
   ** machine without having to enter a password.
   */
 #endif
+  { "umask", DT_NUM, R_NONE, &Umask, 0077 },
+  /*
+  ** .pp
+  ** Sets the umask to use when creating mailboxes or saving attachments.
+  */
+  
   { "use_8bitmime",    DT_BOOL, R_NONE, OPTUSE8BITMIME, 0 },
   /*
   ** .pp
diff -r c36482e98b81 -r d61d41715015 lib.c
--- a/lib.c     Fri Mar 02 20:24:25 2007 -0800
+++ b/lib.c     Fri Mar 02 21:12:33 2007 -0800
@@ -570,7 +570,7 @@ int safe_open (const char *path, int fla
                        safe_dir, sizeof (safe_dir)) == -1)
       return -1;
     
-    if ((fd = open (safe_file, flags, 0600)) < 0)
+    if ((fd = open (safe_file, flags, 0666)) < 0)
     {
       rmdir (safe_dir);
       return fd;
@@ -584,7 +584,7 @@ int safe_open (const char *path, int fla
   }
   else
   {
-    if ((fd = open (path, flags, 0600)) < 0)
+    if ((fd = open (path, flags, 0666)) < 0)
       return fd;
   }
     
diff -r c36482e98b81 -r d61d41715015 mh.c
--- a/mh.c      Fri Mar 02 20:24:25 2007 -0800
+++ b/mh.c      Fri Mar 02 21:12:33 2007 -0800
@@ -207,7 +207,7 @@ static int mh_mkstemp (CONTEXT * dest, F
   {
     snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
              dest->path, NONULL (Hostname), (int) getpid (), Counter++);
-    if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
+    if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
     {
       if (errno != EEXIST)
       {
@@ -1145,7 +1145,7 @@ int maildir_open_new_message (MESSAGE * 
     dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
                path));
 
-    if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
+    if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
     {
       if (errno != EEXIST)
       {
diff -r c36482e98b81 -r d61d41715015 mx.c
--- a/mx.c      Fri Mar 02 20:24:25 2007 -0800
+++ b/mx.c      Fri Mar 02 21:12:33 2007 -0800
@@ -492,6 +492,7 @@ static int mx_open_mailbox_append (CONTE
 static int mx_open_mailbox_append (CONTEXT *ctx, int flags)
 {
   struct stat sb;
+  mode_t omask;
 
   ctx->append = 1;
 
@@ -502,6 +503,8 @@ static int mx_open_mailbox_append (CONTE
 
 #endif
   
+  omask = umask(Umask);
+
   if(stat(ctx->path, &sb) == 0)
   {
     ctx->magic = mx_get_magic (ctx->path);
@@ -523,33 +526,33 @@ static int mx_open_mailbox_append (CONTE
     {
       char tmp[_POSIX_PATH_MAX];
 
-      if (mkdir (ctx->path, S_IRWXU))
+      if (mkdir (ctx->path, S_IRWXU|S_IRWXG|S_IRWXO))
       {
        mutt_perror (ctx->path);
-       return (-1);
+       goto err_umask;
       }
 
       if (ctx->magic == M_MAILDIR)
       {
        snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
-       if (mkdir (tmp, S_IRWXU))
+       if (mkdir (tmp, S_IRWXU|S_IRWXG|S_IRWXO))
        {
          mutt_perror (tmp);
          rmdir (ctx->path);
-         return (-1);
+         goto err_umask;
        }
 
        snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
-       if (mkdir (tmp, S_IRWXU))
+       if (mkdir (tmp, S_IRWXU|S_IRWXG|S_IRWXO))
        {
          mutt_perror (tmp);
          snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
          rmdir (tmp);
          rmdir (ctx->path);
-         return (-1);
+         goto err_umask;
        }
        snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
-       if (mkdir (tmp, S_IRWXU))
+       if (mkdir (tmp, S_IRWXU|S_IRWXG|S_IRWXO))
        {
          mutt_perror (tmp);
          snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
@@ -557,7 +560,7 @@ static int mx_open_mailbox_append (CONTE
          snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
          rmdir (tmp);
          rmdir (ctx->path);
-         return (-1);
+         goto err_umask;
        }
       }
       else
@@ -565,11 +568,11 @@ static int mx_open_mailbox_append (CONTE
        int i;
 
        snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
-       if ((i = creat (tmp, S_IRWXU)) == -1)
+       if ((i = creat (tmp, S_IRWXU|S_IRWXG|S_IRWXO)) == -1)
        {
          mutt_perror (tmp);
          rmdir (ctx->path);
-         return (-1);
+         goto err_umask;
        }
        close (i);
       }
@@ -578,7 +581,7 @@ static int mx_open_mailbox_append (CONTE
   else
   {
     mutt_perror (ctx->path);
-    return (-1);
+    goto err_umask;
   }
 
   switch (ctx->magic)
@@ -595,7 +598,7 @@ static int mx_open_mailbox_append (CONTE
          mutt_error (_("Couldn't lock %s\n"), ctx->path);
          safe_fclose (&ctx->fp);
        }
-       return (-1);
+       goto err_umask;
       }
       fseek (ctx->fp, 0, 2);
       break;
@@ -606,10 +609,15 @@ static int mx_open_mailbox_append (CONTE
       break;
 
     default:
-      return (-1);
-  }
-
+      goto err_umask;
+  }
+
+  umask(omask);
   return 0;
+
+ err_umask:
+  umask(omask);
+  return -1;
 }
 
 /*
@@ -1258,6 +1266,7 @@ MESSAGE *mx_open_new_message (CONTEXT *d
   MESSAGE *msg;
   int (*func) (MESSAGE *, CONTEXT *, HEADER *);
   ADDRESS *p = NULL;
+  mode_t omask;
 
   switch (dest->magic)
   {
@@ -1296,6 +1305,8 @@ MESSAGE *mx_open_new_message (CONTEXT *d
 
   if(msg->received == 0)
     time(&msg->received);
+
+  omask = umask(Umask);
   
   if (func (msg, dest, hdr) == 0)
   {
@@ -1320,6 +1331,8 @@ MESSAGE *mx_open_new_message (CONTEXT *d
   }
   else
     FREE (&msg);
+
+  umask(omask);
 
   return msg;
 }