meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@0: #include "masqmail.h" meillo@0: #include "peopen.h" meillo@0: #include meillo@0: meillo@10: static void meillo@10: message_stream(FILE * out, message * msg, GList * hdr_list, guint flags) meillo@0: { meillo@10: time_t now = time(NULL); meillo@10: GList *node; meillo@10: meillo@10: if (flags & MSGSTR_FROMLINE) { meillo@10: fprintf(out, "From <%s@%s> %s", msg->return_path->local_part, msg->return_path->domain, ctime(&now)); meillo@10: } meillo@10: meillo@10: foreach(hdr_list, node) { meillo@10: header *hdr = (header *) (node->data); meillo@10: fputs(hdr->header, out); meillo@10: } meillo@10: putc('\n', out); meillo@10: foreach(msg->data_list, node) { meillo@10: /* From hack: */ meillo@10: if (flags & MSGSTR_FROMHACK) { meillo@10: if (strncmp(node->data, "From ", 5) == 0) meillo@10: putc('>', out); meillo@10: } meillo@10: fputs(node->data, out); meillo@10: } meillo@10: putc('\n', out); meillo@0: } meillo@0: meillo@10: gboolean meillo@10: append_file(message * msg, GList * hdr_list, gchar * user) meillo@0: { meillo@10: struct passwd *pw; meillo@10: gboolean ok = FALSE; meillo@0: meillo@10: /* headers may be special for a local delivery */ meillo@10: if (hdr_list == NULL) meillo@10: hdr_list = msg->hdr_list; meillo@0: meillo@10: if ((pw = getpwnam(user))) { meillo@10: uid_t saved_uid = geteuid(); meillo@10: gid_t saved_gid = getegid(); meillo@10: gboolean uid_ok = TRUE, gid_ok = TRUE; meillo@0: meillo@10: if (!conf.run_as_user) { meillo@10: uid_ok = (seteuid(0) == 0); meillo@10: if (uid_ok) { meillo@10: gid_ok = (setegid(conf.mail_gid) == 0); meillo@10: uid_ok = (seteuid(pw->pw_uid) == 0); meillo@10: } meillo@10: } meillo@0: meillo@10: DEBUG(5) debugf("running as euid %d\n", geteuid()); meillo@10: DEBUG(5) debugf("running as egid %d\n", getegid()); meillo@0: meillo@10: if (uid_ok && gid_ok) { meillo@10: gchar *filename; meillo@10: FILE *out; meillo@10: meillo@10: filename = g_strdup_printf("%s/%s", conf.mail_dir, user); meillo@10: if ((out = fopen(filename, "a"))) { meillo@0: #ifdef USE_LIBLOCKFILE meillo@10: gint err; meillo@10: /* lock file using liblockfile */ meillo@10: err = maillock(user, 3); meillo@10: if (err == 0) { meillo@0: #else meillo@10: /* lock file: */ meillo@10: struct flock lock; meillo@10: lock.l_type = F_WRLCK; meillo@10: lock.l_whence = SEEK_END; meillo@10: lock.l_start = lock.l_len = 0; meillo@10: if (fcntl(fileno(out), F_SETLK, &lock) != -1) { meillo@0: #endif meillo@10: fchmod(fileno(out), 0600); meillo@0: meillo@10: message_stream(out, msg, hdr_list, MSGSTR_FROMLINE | MSGSTR_FROMHACK); meillo@10: meillo@10: ok = TRUE; meillo@10: meillo@10: /* close when still user */ meillo@10: fclose(out); meillo@0: #ifdef USE_LIBLOCKFILE meillo@10: mailunlock(); meillo@0: #endif meillo@10: } else { meillo@10: fclose(out); meillo@0: #ifdef USE_LIBLOCKFILE meillo@10: DEBUG(3) debugf("could not lock file %s: error %d\n", filename, err); meillo@10: } /* XEmacs indenting convenience... */ meillo@0: #else meillo@10: DEBUG(3) debugf("could not lock file %s: %s\n", filename, strerror(errno)); meillo@10: } meillo@10: #endif meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "could not open file %s: %s\n", filename, strerror(errno)); meillo@10: } meillo@10: g_free(filename); meillo@10: meillo@10: if (!conf.run_as_user) { meillo@10: uid_ok = (seteuid(0) == 0); meillo@10: if (uid_ok) { meillo@10: gid_ok = (setegid(saved_gid) == 0); meillo@10: uid_ok = (seteuid(saved_uid) == 0); meillo@10: } meillo@10: } meillo@10: meillo@10: if (!uid_ok || !gid_ok) { meillo@10: /* FIXME: if this fails we HAVE to exit, because we shall not run meillo@10: with some users id. But we do not return, and so this message meillo@10: will not be finished, so the user will get the message again meillo@10: next time a delivery is attempted... */ meillo@10: logwrite(LOG_ALERT, "could not set back uid or gid after local delivery: %s\n", strerror(errno)); meillo@10: logwrite(LOG_ALERT, "uid=%d, gid=%d, euid=%d, egid=%d, want = %d, %d\n", meillo@10: getuid(), getgid(), geteuid(), getegid(), saved_uid, saved_gid); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "could not set uid or gid for local delivery, uid = %d: %s\n", pw->pw_uid, strerror(errno)); meillo@10: } meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "could not find password entry for user %s\n", user); meillo@10: errno = ENOENT; /* getpwnam does not set errno correctly */ meillo@0: } meillo@0: meillo@10: return ok; meillo@0: } meillo@0: meillo@0: #ifdef ENABLE_MAILDIR meillo@10: gboolean meillo@10: maildir_out(message * msg, GList * hdr_list, gchar * user, guint flags) meillo@0: { meillo@10: struct passwd *pw; meillo@10: gboolean ok = FALSE; meillo@0: meillo@10: /* headers may be special for a local delivery */ meillo@10: if (hdr_list == NULL) meillo@10: hdr_list = msg->hdr_list; meillo@0: meillo@10: if ((pw = getpwnam(user))) { meillo@10: uid_t saved_uid = geteuid(); meillo@10: gid_t saved_gid = getegid(); meillo@10: gboolean uid_ok = TRUE, gid_ok = TRUE; meillo@0: meillo@10: if (!conf.run_as_user) { meillo@10: uid_ok = (seteuid(0) == 0); meillo@10: if (uid_ok) { meillo@10: gid_ok = (setegid(conf.mail_gid) == 0); meillo@10: uid_ok = (seteuid(pw->pw_uid) == 0); meillo@10: } meillo@10: } meillo@0: meillo@10: DEBUG(5) debugf("running as euid %d\n", geteuid()); meillo@10: DEBUG(5) debugf("running as egid %d\n", getegid()); meillo@0: meillo@10: if (uid_ok && gid_ok) { meillo@10: char *path = g_strdup_printf("%s/Maildir", pw->pw_dir); meillo@10: struct stat statbuf; meillo@10: int ret; meillo@0: meillo@10: DEBUG(5) debugf("path = %s\n", path); meillo@10: meillo@10: ok = TRUE; meillo@10: ret = stat(path, &statbuf); meillo@10: if (ret != 0) { meillo@10: ok = FALSE; meillo@10: if (errno == ENOENT) { meillo@10: logwrite(LOG_NOTICE, "directory %s does not exist, creating\n", path); meillo@10: if (mkdir(path, 0700) == 0) meillo@10: ok = TRUE; meillo@10: } else meillo@10: logwrite(LOG_ALERT, "stat of %s failed: %s\n", path, strerror(errno)); meillo@10: } meillo@10: if (ok) { meillo@10: ok = FALSE; meillo@10: ret = stat(path, &statbuf); meillo@10: if (S_ISDIR(statbuf.st_mode)) { meillo@10: gchar *subdirs[] = { "tmp", "new", "cur" }; meillo@10: int i; meillo@10: for (i = 0; i < 3; i++) { meillo@10: char *path1 = g_strdup_printf("%s/%s", path, subdirs[i]); meillo@10: ret = stat(path1, &statbuf); meillo@10: if (ret != 0) { meillo@10: if (errno == ENOENT) { meillo@10: logwrite(LOG_NOTICE, "directory %s does not exist, creating\n", path1); meillo@10: if (mkdir(path1, 0700) != 0) meillo@10: break; meillo@10: } meillo@10: } meillo@10: g_free(path1); meillo@10: } meillo@10: if (i == 3) { meillo@10: FILE *out; meillo@10: mode_t saved_mode = umask(066); meillo@10: /* the qmail style unique works only if delivering meillo@10: with different process. We do not fork for each delivery, meillo@10: so our uid is more unique. Hope it is compatible with all meillo@10: MUAs. meillo@10: */ meillo@10: gchar *filename = g_strdup_printf("%s/tmp/%s.%s", path, msg->uid, conf.host_name); meillo@10: meillo@10: DEBUG(5) debugf("filename = %s\n", filename); meillo@10: meillo@10: if ((out = fopen(filename, "w"))) { meillo@10: gchar *newname = g_strdup_printf("%s/new/%s.%s", path, msg->uid, conf.host_name); meillo@10: message_stream(out, msg, hdr_list, flags); meillo@10: ok = TRUE; meillo@10: if (fflush(out) == EOF) meillo@10: ok = FALSE; meillo@10: else if (fdatasync(fileno(out)) != 0) { meillo@10: if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */ meillo@10: ok = FALSE; meillo@10: } meillo@10: fclose(out); meillo@10: if (rename(filename, newname) != 0) { meillo@10: ok = FALSE; meillo@10: logwrite(LOG_ALERT, "moving %s to %s failed: %s", filename, newname, strerror(errno)); meillo@10: } meillo@10: g_free(newname); meillo@10: } meillo@10: umask(saved_mode); meillo@10: g_free(filename); meillo@10: } meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "%s is not a directory\n", path); meillo@10: errno = ENOTDIR; meillo@10: } meillo@10: } meillo@10: if (!conf.run_as_user) { meillo@10: uid_ok = (seteuid(0) == 0); meillo@10: if (uid_ok) { meillo@10: gid_ok = (setegid(saved_gid) == 0); meillo@10: uid_ok = (seteuid(saved_uid) == 0); meillo@10: } meillo@10: } meillo@10: if (!uid_ok || !gid_ok) { meillo@10: /* FIXME: if this fails we HAVE to exit, because we shall not run meillo@10: with some users id. But we do not return, and so this message meillo@10: will not be finished, so the user will get the message again meillo@10: next time a delivery is attempted... */ meillo@10: logwrite(LOG_ALERT, "could not set back uid or gid after local delivery: %s\n", strerror(errno)); meillo@10: exit(EXIT_FAILURE); meillo@10: } meillo@10: g_free(path); meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "could not set uid or gid for local delivery, uid = %d: %s\n", pw->pw_uid, strerror(errno)); meillo@10: } meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "could not find password entry for user %s\n", user); meillo@10: errno = ENOENT; /* getpwnam does not set errno correctly */ meillo@0: } meillo@10: return ok; meillo@0: } meillo@0: #endif meillo@0: meillo@0: gboolean meillo@10: pipe_out(message * msg, GList * hdr_list, address * rcpt, gchar * cmd, guint flags) meillo@0: { meillo@10: gchar *envp[40]; meillo@10: FILE *out; meillo@10: uid_t saved_uid = geteuid(); meillo@10: gid_t saved_gid = getegid(); meillo@10: gboolean ok = FALSE; meillo@10: gint i, n; meillo@10: pid_t pid; meillo@10: void (*old_signal) (int); meillo@10: int status; meillo@0: meillo@10: /* set uid and gid to the mail ids */ meillo@10: if (!conf.run_as_user) { meillo@10: set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); meillo@10: } meillo@0: meillo@10: /* set environment */ meillo@10: { meillo@10: gint i = 0; meillo@10: address *ancestor = addr_find_ancestor(rcpt); meillo@0: meillo@10: envp[i++] = g_strdup_printf("SENDER=%s@%s", msg->return_path->local_part, msg->return_path->domain); meillo@10: envp[i++] = g_strdup_printf("SENDER_DOMAIN=%s", msg->return_path->domain); meillo@10: envp[i++] = g_strdup_printf("SENDER_LOCAL=%s", msg->return_path->local_part); meillo@10: envp[i++] = g_strdup_printf("RECEIVED_HOST=%s", msg->received_host ? msg->received_host : ""); meillo@0: meillo@10: envp[i++] = g_strdup_printf("RETURN_PATH=%s@%s", msg->return_path->local_part, msg->return_path->domain); meillo@10: envp[i++] = g_strdup_printf("DOMAIN=%s", ancestor->domain); meillo@0: meillo@10: envp[i++] = g_strdup_printf("LOCAL_PART=%s", ancestor->local_part); meillo@10: envp[i++] = g_strdup_printf("USER=%s", ancestor->local_part); meillo@10: envp[i++] = g_strdup_printf("LOGNAME=%s", ancestor->local_part); meillo@0: meillo@10: envp[i++] = g_strdup_printf("MESSAGE_ID=%s", msg->uid); meillo@10: envp[i++] = g_strdup_printf("QUALIFY_DOMAIN=%s", conf.host_name); meillo@0: meillo@10: envp[i] = NULL; meillo@10: n = i; meillo@10: } meillo@0: meillo@10: old_signal = signal(SIGCHLD, SIG_DFL); meillo@0: meillo@10: out = peidopen(cmd, "w", envp, &pid, conf.mail_uid, conf.mail_gid); meillo@10: if (out != NULL) { meillo@10: message_stream(out, msg, hdr_list, flags); meillo@0: meillo@10: fclose(out); meillo@0: meillo@10: waitpid(pid, &status, 0); meillo@0: meillo@10: if (WEXITSTATUS(status) != 0) { meillo@10: int exstat = WEXITSTATUS(status); meillo@10: logwrite(LOG_ALERT, "process returned %d (%s)\n", exstat, ext_strerror(1024 + exstat)); meillo@10: errno = 1024 + exstat; meillo@10: } else if (WIFSIGNALED(status)) { meillo@10: logwrite(LOG_ALERT, "process got signal %d\n", WTERMSIG(status)); meillo@10: } else meillo@10: ok = TRUE; meillo@0: meillo@10: } else meillo@10: logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", cmd, strerror(errno)); meillo@0: meillo@10: signal(SIGCHLD, old_signal); meillo@0: meillo@10: /* free environment */ meillo@10: for (i = 0; i < n; i++) { meillo@10: g_free(envp[i]); meillo@10: } meillo@0: meillo@10: /* set uid and gid back */ meillo@10: if (!conf.run_as_user) { meillo@10: set_euidgid(saved_uid, saved_gid, NULL, NULL); meillo@10: } meillo@0: meillo@10: return ok; meillo@0: }