int delivery(const char *mailbox) { FormatMbox format_mbox; if (format_mbox.HasMsg()) return (0); DeliverDotLock dotlock; Buffer b; if ( *mailbox == '!' || *mailbox == '|' ) { Buffer cmdbuf; if (*mailbox == '!') { b="SENDMAIL"; const char *sendmail=GetVarStr(b); cmdbuf=sendmail; cmdbuf += " -f '' "; cmdbuf += mailbox+1; } else cmdbuf= mailbox+1; cmdbuf += '\0'; if (VerboseLevel() > 0) merr << "maildrop: Delivering to |" << (const char *)cmdbuf << "\n"; PipeFds pipe; if (pipe.Pipe()) throw "Cannot create pipe."; pid_t pid=fork(); if (pid < 0) throw "Cannot fork."; if (pid == 0) { pipe.close1(); dup2(pipe.fds[0], 0); pipe.close0(); try { subshell(cmdbuf); } catch (const char *p) { if (write(2, p, strlen(p)) < 0 || write(2, "\n", 1) < 0) ; /* ignored */ _exit(100); } #if NEED_NONCONST_EXCEPTIONS catch (char *p) { if (write(2, p, strlen(p)) < 0 || write(2, "\n", 1) < 0) ; /* ignored */ _exit(100); } #endif catch (...) { _exit(100); } } Mio pipemio; pipe.close0(); pipemio.fd(pipe.fds[1]); pipe.fds[1]= -1; format_mbox.Init(0); int rc=format_mbox.DeliverTo(pipemio); int wait_stat; while (wait(&wait_stat) != pid) ; if (wait_stat == 0) rc=0; log(mailbox, rc || wait_stat, format_mbox); { Buffer name, val; if (rc) wait_stat= -1; else wait_stat= WIFEXITED(wait_stat) ? WEXITSTATUS(wait_stat):-1; val.append( (unsigned long)wait_stat); name="EXITCODE"; SetVar(name, val); } if (rc) return (-1); } else if (Maildir::IsMaildir(mailbox)) { Maildir deliver_maildir; Mio deliver_file; if ( deliver_maildir.MaildirOpen(mailbox, deliver_file, maildrop.msgptr->MessageSize()) < 0) { #if HAVE_COURIER throw 75; #else throw 77; #endif } format_mbox.Init(0); if (format_mbox.DeliverTo(deliver_file)) { log(mailbox, -1, format_mbox); return (-1); } deliver_maildir.MaildirSave(); log(mailbox, 0, format_mbox); } else // Delivering to a mailbox (hopefully) { if (VerboseLevel() > 0) merr << "maildrop: Delivering to " << mailbox << "\n"; #if USE_DOTLOCK dotlock.LockMailbox(mailbox); #endif struct stat stat_buf; Mio mio; Buffer name_buf; name_buf="UMASK"; const char *um=GetVarStr(name_buf); unsigned int umask_val=077; sscanf(um, "%o", &umask_val); umask_val=umask(umask_val); if (mio.Open(mailbox, O_CREAT | O_WRONLY, 0666) < 0) { umask_val=umask(umask_val); throw "Unable to open mailbox."; } umask_val=umask(umask_val); if (fstat(mio.fd(), &stat_buf) < 0) throw "Unable to open mailbox."; #if USE_FLOCK if (VerboseLevel() > 4) merr << "maildrop: Flock()ing " << mailbox << ".\n"; FileLock::do_filelock(mio.fd()); #endif if (S_ISREG(stat_buf.st_mode)) { if (mio.seek(0L, SEEK_END) < 0) throw "Seek error on mailbox."; dotlock.trap_truncate(mio.fd(), stat_buf.st_size); } if (VerboseLevel() > 4) merr << "maildrop: Appending to " << mailbox << ".\n"; try { format_mbox.Init(1); if ((stat_buf.st_size > 0 && mio.write( #if CRLF_TERM "\r\n", 2 #else "\n", 1 #endif ) < 0) || format_mbox.DeliverTo(mio)) { dotlock.truncate(); log(mailbox, -1, format_mbox); return (-1); } } catch (...) { dotlock.truncate(); log(mailbox, -1, format_mbox); throw; } log(mailbox, 0, format_mbox); } if (VerboseLevel() > 4) merr << "maildrop: Delivery complete.\n"; return (0); }
int Maildir::MaildirOpen(const char *dir, Mio &file, off_t s) { Buffer buf; struct maildirsize quotainfo; const char *quotap; Buffer quotabuf; quotabuf="MAILDIRQUOTA"; /* Reuse a convenient buffer */ quotabuf= *GetVar(quotabuf); quotabuf += '\0'; quotap=quotabuf; if (!*quotap) quotap=NULL; MaildirAbort(); AlarmTimer abort_timer; static long counter=0; buf.set(counter++); buf += '\0'; struct maildir_tmpcreate_info createInfo; maildir_tmpcreate_init(&createInfo); createInfo.maildir=dir; createInfo.uniq=(const char *)buf; createInfo.msgsize=s; createInfo.openmode=0666; abort_timer.Set( 24 * 60 * 60 ); while (!abort_timer.Expired()) { Buffer name_buf; name_buf="UMASK"; const char *um=GetVarStr(name_buf); unsigned int umask_val=077; sscanf(um, "%o", &umask_val); umask_val=umask(umask_val); int f=maildir_tmpcreate_fd(&createInfo); umask(umask_val); if (f >= 0) { Buffer b; b="FLAGS"; const char *flags=GetVarStr(b); tmpname=createInfo.tmpname; tmpname += '\0'; if (flags) { const char *p=flags; while (*p) { if (strchr("DRSF", *p) == NULL) { f=0; break; } ++p; } } if (flags && *flags) { newname=createInfo.curname; newname += ':'; newname += '2'; newname += ','; newname += flags; } else { newname=createInfo.newname; } newname += '\0'; maildir_tmpcreate_free(&createInfo); file.fd(f); is_open=1; maildirRoot=dir; maildirRoot += '\0'; if (maildir_quota_add_start(dir, "ainfo, s, 1, quotap)) { file.fd(-1); unlink( (const char *)tmpname ); is_open=0; maildir_deliver_quota_warning(dir, quota_warn_percent, quota_warn_message); merr << "maildrop: maildir over quota.\n"; return (-1); } maildir_quota_add_end("ainfo, s, 1); return (0); } if (errno != EAGAIN) { merr << "maildrop: " << dir << ": " << strerror(errno) << "\n"; return -1; } AlarmSleep try_again(2); } merr << "maildrop: time out on maildir directory.\n"; return (-1); }