Commit ffaf59d9 ffaf59d9d2bfdd1282d477e4b27791294bc92f81 by Sergey Poznyakoff

Call util_save_outgoing() to preserve the message.

Set umask 077 before calling mkstemp().
1 parent 46aac409
...@@ -57,14 +57,20 @@ mail_send (int argc, char **argv) ...@@ -57,14 +57,20 @@ mail_send (int argc, char **argv)
57 else 57 else
58 subj = (util_find_env ("subject"))->value; 58 subj = (util_find_env ("subject"))->value;
59 59
60 return mail_send0 (to, cc, bcc, subj); 60 return mail_send0 (to, cc, bcc, subj, isupper(argv[0][0]));
61 } 61 }
62 62
63 /* Shared between mail_send() and mail_reply(); 63 /* mail_send0(): shared between mail_send() and mail_reply();
64 NOTE: arguments should be allocated dynamically. They will be freed 64
65 If the variable "record" is set, the outgoing message is
66 saved after being sent. If "save_to" argument is non-zero,
67 the name of the save file is derived from "to" argument. Otherwise,
68 it is taken from the value of "record" variable.
69
70 NOTE: arguments must be allocated dynamically. They will be freed
65 before exit */ 71 before exit */
66 int 72 int
67 mail_send0 (char *to, char *cc, char *bcc, char *subj) 73 mail_send0 (char *to, char *cc, char *bcc, char *subj, int save_to)
68 { 74 {
69 char *buf = NULL; 75 char *buf = NULL;
70 size_t n = 0; 76 size_t n = 0;
...@@ -73,17 +79,22 @@ mail_send0 (char *to, char *cc, char *bcc, char *subj) ...@@ -73,17 +79,22 @@ mail_send0 (char *to, char *cc, char *bcc, char *subj)
73 char *filename; 79 char *filename;
74 FILE *file; 80 FILE *file;
75 const char *tmpdir; 81 const char *tmpdir;
76 82 char *savefile = NULL;
83
77 /* We have to be extra careful about opening temporary files, since we 84 /* We have to be extra careful about opening temporary files, since we
78 may be running with extra privilege i.e setgid(). */ 85 may be running with extra privilege i.e setgid(). */
79 tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : "/tmp"; 86 tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : "/tmp";
80 filename = alloca (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1); 87 filename = alloca (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
81 sprintf (filename, "%s/muXXXXXX", tmpdir); 88 sprintf (filename, "%s/muXXXXXX", tmpdir);
82 #ifdef HAVE_MKSTEMP 89 #ifdef HAVE_MKSTEMP
83 fd = mkstemp (filename); 90 {
91 int save_mask = umask(077);
92 fd = mkstemp (filename);
93 umask(save_mask);
94 }
84 #else 95 #else
85 if (mktemp (filename)) 96 if (mktemp (filename))
86 fd = open(filename, O_CREAT|O_EXCL|O_WRONLY, 0600); 97 fd = open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
87 else 98 else
88 fd = -1; 99 fd = -1;
89 #endif 100 #endif
...@@ -94,7 +105,7 @@ mail_send0 (char *to, char *cc, char *bcc, char *subj) ...@@ -94,7 +105,7 @@ mail_send0 (char *to, char *cc, char *bcc, char *subj)
94 return 1; 105 return 1;
95 } 106 }
96 107
97 file = fdopen (fd, "w"); 108 file = fdopen (fd, "w+");
98 109
99 while (getline (&buf, &n, stdin) >= 0 && !done) 110 while (getline (&buf, &n, stdin) >= 0 && !done)
100 { 111 {
...@@ -278,9 +289,25 @@ mail_send0 (char *to, char *cc, char *bcc, char *subj) ...@@ -278,9 +289,25 @@ mail_send0 (char *to, char *cc, char *bcc, char *subj)
278 free (buf); 289 free (buf);
279 buf = NULL; 290 buf = NULL;
280 } 291 }
281 fclose (file);
282 } 292 }
283 293
294 fclose (file);
295
296 /* Save outgoing message */
297 if (save_to)
298 {
299 savefile = strdup (to);
300 if (savefile)
301 {
302 char *p = strchr(savefile, '@');
303 if (p)
304 *p = 0;
305 }
306 }
307 util_save_outgoing (msg, savefile);
308 if (savefile)
309 free (savefile);
310
284 /* Send the message. */ 311 /* Send the message. */
285 mailer_send_message (mailer, msg); 312 mailer_send_message (mailer, msg);
286 message_destroy (&msg, NULL); 313 message_destroy (&msg, NULL);
......