masqmail

annotate src/local.c @ 11:24872a9fe6e1

merged
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:31 +0100
parents 08114f7dcc23
children f671821d8222
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 1999-2001 Oliver Kurth
meillo@0 3
meillo@0 4 This program is free software; you can redistribute it and/or modify
meillo@0 5 it under the terms of the GNU General Public License as published by
meillo@0 6 the Free Software Foundation; either version 2 of the License, or
meillo@0 7 (at your option) any later version.
meillo@0 8
meillo@0 9 This program is distributed in the hope that it will be useful,
meillo@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 GNU General Public License for more details.
meillo@0 13
meillo@0 14 You should have received a copy of the GNU General Public License
meillo@0 15 along with this program; if not, write to the Free Software
meillo@0 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include "masqmail.h"
meillo@0 20 #include "peopen.h"
meillo@0 21 #include <sys/wait.h>
meillo@0 22
meillo@10 23 static void
meillo@10 24 message_stream(FILE * out, message * msg, GList * hdr_list, guint flags)
meillo@0 25 {
meillo@10 26 time_t now = time(NULL);
meillo@10 27 GList *node;
meillo@10 28
meillo@10 29 if (flags & MSGSTR_FROMLINE) {
meillo@10 30 fprintf(out, "From <%s@%s> %s", msg->return_path->local_part, msg->return_path->domain, ctime(&now));
meillo@10 31 }
meillo@10 32
meillo@10 33 foreach(hdr_list, node) {
meillo@10 34 header *hdr = (header *) (node->data);
meillo@10 35 fputs(hdr->header, out);
meillo@10 36 }
meillo@10 37 putc('\n', out);
meillo@10 38 foreach(msg->data_list, node) {
meillo@10 39 /* From hack: */
meillo@10 40 if (flags & MSGSTR_FROMHACK) {
meillo@10 41 if (strncmp(node->data, "From ", 5) == 0)
meillo@10 42 putc('>', out);
meillo@10 43 }
meillo@10 44 fputs(node->data, out);
meillo@10 45 }
meillo@10 46 putc('\n', out);
meillo@0 47 }
meillo@0 48
meillo@10 49 gboolean
meillo@10 50 append_file(message * msg, GList * hdr_list, gchar * user)
meillo@0 51 {
meillo@10 52 struct passwd *pw;
meillo@10 53 gboolean ok = FALSE;
meillo@0 54
meillo@10 55 /* headers may be special for a local delivery */
meillo@10 56 if (hdr_list == NULL)
meillo@10 57 hdr_list = msg->hdr_list;
meillo@0 58
meillo@10 59 if ((pw = getpwnam(user))) {
meillo@10 60 uid_t saved_uid = geteuid();
meillo@10 61 gid_t saved_gid = getegid();
meillo@10 62 gboolean uid_ok = TRUE, gid_ok = TRUE;
meillo@0 63
meillo@10 64 if (!conf.run_as_user) {
meillo@10 65 uid_ok = (seteuid(0) == 0);
meillo@10 66 if (uid_ok) {
meillo@10 67 gid_ok = (setegid(conf.mail_gid) == 0);
meillo@10 68 uid_ok = (seteuid(pw->pw_uid) == 0);
meillo@10 69 }
meillo@10 70 }
meillo@0 71
meillo@10 72 DEBUG(5) debugf("running as euid %d\n", geteuid());
meillo@10 73 DEBUG(5) debugf("running as egid %d\n", getegid());
meillo@0 74
meillo@10 75 if (uid_ok && gid_ok) {
meillo@10 76 gchar *filename;
meillo@10 77 FILE *out;
meillo@10 78
meillo@10 79 filename = g_strdup_printf("%s/%s", conf.mail_dir, user);
meillo@10 80 if ((out = fopen(filename, "a"))) {
meillo@0 81 #ifdef USE_LIBLOCKFILE
meillo@10 82 gint err;
meillo@10 83 /* lock file using liblockfile */
meillo@10 84 err = maillock(user, 3);
meillo@10 85 if (err == 0) {
meillo@0 86 #else
meillo@10 87 /* lock file: */
meillo@10 88 struct flock lock;
meillo@10 89 lock.l_type = F_WRLCK;
meillo@10 90 lock.l_whence = SEEK_END;
meillo@10 91 lock.l_start = lock.l_len = 0;
meillo@10 92 if (fcntl(fileno(out), F_SETLK, &lock) != -1) {
meillo@0 93 #endif
meillo@10 94 fchmod(fileno(out), 0600);
meillo@0 95
meillo@10 96 message_stream(out, msg, hdr_list, MSGSTR_FROMLINE | MSGSTR_FROMHACK);
meillo@10 97
meillo@10 98 ok = TRUE;
meillo@10 99
meillo@10 100 /* close when still user */
meillo@10 101 fclose(out);
meillo@0 102 #ifdef USE_LIBLOCKFILE
meillo@10 103 mailunlock();
meillo@0 104 #endif
meillo@10 105 } else {
meillo@10 106 fclose(out);
meillo@0 107 #ifdef USE_LIBLOCKFILE
meillo@10 108 DEBUG(3) debugf("could not lock file %s: error %d\n", filename, err);
meillo@10 109 } /* XEmacs indenting convenience... */
meillo@0 110 #else
meillo@10 111 DEBUG(3) debugf("could not lock file %s: %s\n", filename, strerror(errno));
meillo@10 112 }
meillo@10 113 #endif
meillo@10 114 } else {
meillo@10 115 logwrite(LOG_ALERT, "could not open file %s: %s\n", filename, strerror(errno));
meillo@10 116 }
meillo@10 117 g_free(filename);
meillo@10 118
meillo@10 119 if (!conf.run_as_user) {
meillo@10 120 uid_ok = (seteuid(0) == 0);
meillo@10 121 if (uid_ok) {
meillo@10 122 gid_ok = (setegid(saved_gid) == 0);
meillo@10 123 uid_ok = (seteuid(saved_uid) == 0);
meillo@10 124 }
meillo@10 125 }
meillo@10 126
meillo@10 127 if (!uid_ok || !gid_ok) {
meillo@10 128 /* FIXME: if this fails we HAVE to exit, because we shall not run
meillo@10 129 with some users id. But we do not return, and so this message
meillo@10 130 will not be finished, so the user will get the message again
meillo@10 131 next time a delivery is attempted... */
meillo@10 132 logwrite(LOG_ALERT, "could not set back uid or gid after local delivery: %s\n", strerror(errno));
meillo@10 133 logwrite(LOG_ALERT, "uid=%d, gid=%d, euid=%d, egid=%d, want = %d, %d\n",
meillo@10 134 getuid(), getgid(), geteuid(), getegid(), saved_uid, saved_gid);
meillo@10 135 exit(EXIT_FAILURE);
meillo@10 136 }
meillo@10 137 } else {
meillo@10 138 logwrite(LOG_ALERT, "could not set uid or gid for local delivery, uid = %d: %s\n", pw->pw_uid, strerror(errno));
meillo@10 139 }
meillo@10 140 } else {
meillo@10 141 logwrite(LOG_ALERT, "could not find password entry for user %s\n", user);
meillo@10 142 errno = ENOENT; /* getpwnam does not set errno correctly */
meillo@0 143 }
meillo@0 144
meillo@10 145 return ok;
meillo@0 146 }
meillo@0 147
meillo@0 148 #ifdef ENABLE_MAILDIR
meillo@10 149 gboolean
meillo@10 150 maildir_out(message * msg, GList * hdr_list, gchar * user, guint flags)
meillo@0 151 {
meillo@10 152 struct passwd *pw;
meillo@10 153 gboolean ok = FALSE;
meillo@0 154
meillo@10 155 /* headers may be special for a local delivery */
meillo@10 156 if (hdr_list == NULL)
meillo@10 157 hdr_list = msg->hdr_list;
meillo@0 158
meillo@10 159 if ((pw = getpwnam(user))) {
meillo@10 160 uid_t saved_uid = geteuid();
meillo@10 161 gid_t saved_gid = getegid();
meillo@10 162 gboolean uid_ok = TRUE, gid_ok = TRUE;
meillo@0 163
meillo@10 164 if (!conf.run_as_user) {
meillo@10 165 uid_ok = (seteuid(0) == 0);
meillo@10 166 if (uid_ok) {
meillo@10 167 gid_ok = (setegid(conf.mail_gid) == 0);
meillo@10 168 uid_ok = (seteuid(pw->pw_uid) == 0);
meillo@10 169 }
meillo@10 170 }
meillo@0 171
meillo@10 172 DEBUG(5) debugf("running as euid %d\n", geteuid());
meillo@10 173 DEBUG(5) debugf("running as egid %d\n", getegid());
meillo@0 174
meillo@10 175 if (uid_ok && gid_ok) {
meillo@10 176 char *path = g_strdup_printf("%s/Maildir", pw->pw_dir);
meillo@10 177 struct stat statbuf;
meillo@10 178 int ret;
meillo@0 179
meillo@10 180 DEBUG(5) debugf("path = %s\n", path);
meillo@10 181
meillo@10 182 ok = TRUE;
meillo@10 183 ret = stat(path, &statbuf);
meillo@10 184 if (ret != 0) {
meillo@10 185 ok = FALSE;
meillo@10 186 if (errno == ENOENT) {
meillo@10 187 logwrite(LOG_NOTICE, "directory %s does not exist, creating\n", path);
meillo@10 188 if (mkdir(path, 0700) == 0)
meillo@10 189 ok = TRUE;
meillo@10 190 } else
meillo@10 191 logwrite(LOG_ALERT, "stat of %s failed: %s\n", path, strerror(errno));
meillo@10 192 }
meillo@10 193 if (ok) {
meillo@10 194 ok = FALSE;
meillo@10 195 ret = stat(path, &statbuf);
meillo@10 196 if (S_ISDIR(statbuf.st_mode)) {
meillo@10 197 gchar *subdirs[] = { "tmp", "new", "cur" };
meillo@10 198 int i;
meillo@10 199 for (i = 0; i < 3; i++) {
meillo@10 200 char *path1 = g_strdup_printf("%s/%s", path, subdirs[i]);
meillo@10 201 ret = stat(path1, &statbuf);
meillo@10 202 if (ret != 0) {
meillo@10 203 if (errno == ENOENT) {
meillo@10 204 logwrite(LOG_NOTICE, "directory %s does not exist, creating\n", path1);
meillo@10 205 if (mkdir(path1, 0700) != 0)
meillo@10 206 break;
meillo@10 207 }
meillo@10 208 }
meillo@10 209 g_free(path1);
meillo@10 210 }
meillo@10 211 if (i == 3) {
meillo@10 212 FILE *out;
meillo@10 213 mode_t saved_mode = umask(066);
meillo@10 214 /* the qmail style unique works only if delivering
meillo@10 215 with different process. We do not fork for each delivery,
meillo@10 216 so our uid is more unique. Hope it is compatible with all
meillo@10 217 MUAs.
meillo@10 218 */
meillo@10 219 gchar *filename = g_strdup_printf("%s/tmp/%s.%s", path, msg->uid, conf.host_name);
meillo@10 220
meillo@10 221 DEBUG(5) debugf("filename = %s\n", filename);
meillo@10 222
meillo@10 223 if ((out = fopen(filename, "w"))) {
meillo@10 224 gchar *newname = g_strdup_printf("%s/new/%s.%s", path, msg->uid, conf.host_name);
meillo@10 225 message_stream(out, msg, hdr_list, flags);
meillo@10 226 ok = TRUE;
meillo@10 227 if (fflush(out) == EOF)
meillo@10 228 ok = FALSE;
meillo@10 229 else if (fdatasync(fileno(out)) != 0) {
meillo@10 230 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */
meillo@10 231 ok = FALSE;
meillo@10 232 }
meillo@10 233 fclose(out);
meillo@10 234 if (rename(filename, newname) != 0) {
meillo@10 235 ok = FALSE;
meillo@10 236 logwrite(LOG_ALERT, "moving %s to %s failed: %s", filename, newname, strerror(errno));
meillo@10 237 }
meillo@10 238 g_free(newname);
meillo@10 239 }
meillo@10 240 umask(saved_mode);
meillo@10 241 g_free(filename);
meillo@10 242 }
meillo@10 243 } else {
meillo@10 244 logwrite(LOG_ALERT, "%s is not a directory\n", path);
meillo@10 245 errno = ENOTDIR;
meillo@10 246 }
meillo@10 247 }
meillo@10 248 if (!conf.run_as_user) {
meillo@10 249 uid_ok = (seteuid(0) == 0);
meillo@10 250 if (uid_ok) {
meillo@10 251 gid_ok = (setegid(saved_gid) == 0);
meillo@10 252 uid_ok = (seteuid(saved_uid) == 0);
meillo@10 253 }
meillo@10 254 }
meillo@10 255 if (!uid_ok || !gid_ok) {
meillo@10 256 /* FIXME: if this fails we HAVE to exit, because we shall not run
meillo@10 257 with some users id. But we do not return, and so this message
meillo@10 258 will not be finished, so the user will get the message again
meillo@10 259 next time a delivery is attempted... */
meillo@10 260 logwrite(LOG_ALERT, "could not set back uid or gid after local delivery: %s\n", strerror(errno));
meillo@10 261 exit(EXIT_FAILURE);
meillo@10 262 }
meillo@10 263 g_free(path);
meillo@10 264 } else {
meillo@10 265 logwrite(LOG_ALERT, "could not set uid or gid for local delivery, uid = %d: %s\n", pw->pw_uid, strerror(errno));
meillo@10 266 }
meillo@10 267 } else {
meillo@10 268 logwrite(LOG_ALERT, "could not find password entry for user %s\n", user);
meillo@10 269 errno = ENOENT; /* getpwnam does not set errno correctly */
meillo@0 270 }
meillo@10 271 return ok;
meillo@0 272 }
meillo@0 273 #endif
meillo@0 274
meillo@0 275 gboolean
meillo@10 276 pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags)
meillo@0 277 {
meillo@10 278 gchar *envp[40];
meillo@10 279 FILE *out;
meillo@10 280 uid_t saved_uid = geteuid();
meillo@10 281 gid_t saved_gid = getegid();
meillo@10 282 gboolean ok = FALSE;
meillo@10 283 gint i, n;
meillo@10 284 pid_t pid;
meillo@10 285 void (*old_signal) (int);
meillo@10 286 int status;
meillo@0 287
meillo@10 288 /* set uid and gid to the mail ids */
meillo@10 289 if (!conf.run_as_user) {
meillo@10 290 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
meillo@10 291 }
meillo@0 292
meillo@10 293 /* set environment */
meillo@10 294 {
meillo@10 295 gint i = 0;
meillo@10 296 address *ancestor = addr_find_ancestor(rcpt);
meillo@0 297
meillo@10 298 envp[i++] = g_strdup_printf("SENDER=%s@%s", msg->return_path->local_part, msg->return_path->domain);
meillo@10 299 envp[i++] = g_strdup_printf("SENDER_DOMAIN=%s", msg->return_path->domain);
meillo@10 300 envp[i++] = g_strdup_printf("SENDER_LOCAL=%s", msg->return_path->local_part);
meillo@10 301 envp[i++] = g_strdup_printf("RECEIVED_HOST=%s", msg->received_host ? msg->received_host : "");
meillo@0 302
meillo@10 303 envp[i++] = g_strdup_printf("RETURN_PATH=%s@%s", msg->return_path->local_part, msg->return_path->domain);
meillo@10 304 envp[i++] = g_strdup_printf("DOMAIN=%s", ancestor->domain);
meillo@0 305
meillo@10 306 envp[i++] = g_strdup_printf("LOCAL_PART=%s", ancestor->local_part);
meillo@10 307 envp[i++] = g_strdup_printf("USER=%s", ancestor->local_part);
meillo@10 308 envp[i++] = g_strdup_printf("LOGNAME=%s", ancestor->local_part);
meillo@0 309
meillo@10 310 envp[i++] = g_strdup_printf("MESSAGE_ID=%s", msg->uid);
meillo@10 311 envp[i++] = g_strdup_printf("QUALIFY_DOMAIN=%s", conf.host_name);
meillo@0 312
meillo@10 313 envp[i] = NULL;
meillo@10 314 n = i;
meillo@10 315 }
meillo@0 316
meillo@10 317 old_signal = signal(SIGCHLD, SIG_DFL);
meillo@0 318
meillo@10 319 out = peidopen(cmd, "w", envp, &pid, conf.mail_uid, conf.mail_gid);
meillo@10 320 if (out != NULL) {
meillo@10 321 message_stream(out, msg, hdr_list, flags);
meillo@0 322
meillo@10 323 fclose(out);
meillo@0 324
meillo@10 325 waitpid(pid, &status, 0);
meillo@0 326
meillo@10 327 if (WEXITSTATUS(status) != 0) {
meillo@10 328 int exstat = WEXITSTATUS(status);
meillo@10 329 logwrite(LOG_ALERT, "process returned %d (%s)\n", exstat, ext_strerror(1024 + exstat));
meillo@10 330 errno = 1024 + exstat;
meillo@10 331 } else if (WIFSIGNALED(status)) {
meillo@10 332 logwrite(LOG_ALERT, "process got signal %d\n", WTERMSIG(status));
meillo@10 333 } else
meillo@10 334 ok = TRUE;
meillo@0 335
meillo@10 336 } else
meillo@10 337 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", cmd, strerror(errno));
meillo@0 338
meillo@10 339 signal(SIGCHLD, old_signal);
meillo@0 340
meillo@10 341 /* free environment */
meillo@10 342 for (i = 0; i < n; i++) {
meillo@10 343 g_free(envp[i]);
meillo@10 344 }
meillo@0 345
meillo@10 346 /* set uid and gid back */
meillo@10 347 if (!conf.run_as_user) {
meillo@10 348 set_euidgid(saved_uid, saved_gid, NULL, NULL);
meillo@10 349 }
meillo@0 350
meillo@10 351 return ok;
meillo@0 352 }