commands/vdeliver.cc
changeset 2 b3afb9f1e801
parent 0 6f7a81934006
equal deleted inserted replaced
1:30113bfbe723 2:b3afb9f1e801
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
     1 // Copyright (C) 1999,2000,2005 Bruce Guenter <bruceg@em.ca>
     2 //
     2 //
     3 // This program is free software; you can redistribute it and/or modify
     3 // This program is free software; you can redistribute it and/or modify
     4 // it under the terms of the GNU General Public License as published by
     4 // it under the terms of the GNU General Public License as published by
     5 // the Free Software Foundation; either version 2 of the License, or
     5 // the Free Software Foundation; either version 2 of the License, or
     6 // (at your option) any later version.
     6 // (at your option) any later version.
    21 #include <sys/stat.h>
    21 #include <sys/stat.h>
    22 #include "ac/time.h"
    22 #include "ac/time.h"
    23 #include "ac/wait.h"
    23 #include "ac/wait.h"
    24 #include <signal.h>
    24 #include <signal.h>
    25 #include "fdbuf/fdbuf.h"
    25 #include "fdbuf/fdbuf.h"
    26 #include "cli/cli.h"
    26 #include "cli++/cli++.h"
    27 #include "vcommand.h"
    27 #include "vcommand.h"
    28 #include "misc/itoa.h"
    28 #include "misc/itoa.h"
    29 #include "misc/stat_fns.h"
    29 #include "misc/stat_fns.h"
    30 #include "misc/exec.h"
    30 #include "misc/exec.h"
    31 
    31 
    37 const int cli_args_max = 0;
    37 const int cli_args_max = 0;
    38 static int addufline = false;
    38 static int addufline = false;
    39 static int addrpline = true;
    39 static int addrpline = true;
    40 static int adddtline = true;
    40 static int adddtline = true;
    41 static int o_quiet = false;
    41 static int o_quiet = false;
       
    42 static int run_predeliver = true;
       
    43 static int run_postdeliver = true;
    42 
    44 
    43 // vdeliver is the unified e-mail message delivery agent for virtual
    45 // vdeliver is the unified e-mail message delivery agent for virtual
    44 // domains managed by vmailmgr.
    46 // domains managed by vmailmgr.
    45 // It is run from the F<.qmail-default> file, and automatically handles
    47 // It is run from the F<.qmail-default> file, and automatically handles
    46 // delivery to any user within a virtual domain.
    48 // delivery to any user within a virtual domain.
    59   { 'f', 0, cli_option::flag, false, &addufline,
    61   { 'f', 0, cli_option::flag, false, &addufline,
    60     "Do not add the \"From \" mailbox line (default)", 0 },
    62     "Do not add the \"From \" mailbox line (default)", 0 },
    61   // Do not add the C<From> mailbox line to the top of the message.
    63   // Do not add the C<From> mailbox line to the top of the message.
    62   // Note that this line is never added when the message is being
    64   // Note that this line is never added when the message is being
    63   // re-injected into the mail stream. (default)
    65   // re-injected into the mail stream. (default)
       
    66   { 0, "no-predeliver", cli_option::flag, false, &run_predeliver,
       
    67     "Do not run vdeliver-predeliver scripts", 0 },
       
    68   { 0, "no-postdeliver", cli_option::flag, false, &run_postdeliver,
       
    69     "Do not run vdeliver-postdeliver scripts", 0 },
    64   { 0, "quiet", cli_option::flag, true, &o_quiet,
    70   { 0, "quiet", cli_option::flag, true, &o_quiet,
    65     "Suppress all status messages", 0 },
    71     "Suppress all status messages", 0 },
    66   { 'r', 0, cli_option::flag, false, &addrpline,
    72   { 'r', 0, cli_option::flag, false, &addrpline,
    67     "Do not add the \"Return-Path:\" line", 0 },
    73     "Do not add the \"Return-Path:\" line", 0 },
    68   // Do not add the C<Return-Path:> line to the top of the message.
    74   // Do not add the C<Return-Path:> line to the top of the message.
   313   if(!addrpline)
   319   if(!addrpline)
   314     rpline = 0;
   320     rpline = 0;
   315   if(!adddtline)
   321   if(!adddtline)
   316     dtline = 0;
   322     dtline = 0;
   317 
   323 
   318   vpwentry* vpw = domain.lookup(ext, false);
   324   vpwentry* vpw = domain.lookup(ext);
       
   325   if(!vpw)
       
   326     vpw = domain.lookup(config->default_username());
   319   if(!vpw)
   327   if(!vpw)
   320     die_fail(mystring("Invalid or unknown virtual user '" + ext + "'").c_str());
   328     die_fail(mystring("Invalid or unknown virtual user '" + ext + "'").c_str());
   321   if(vpw->expiry < (unsigned)time(0))
   329   if(vpw->expiry < (unsigned)time(0))
   322     die_fail(mystring("Virtual user '" + ext + "' has expired").c_str());
   330     die_fail(mystring("Virtual user '" + ext + "' has expired").c_str());
   323   
   331   
   324   vpw->export_env();
   332   vpw->export_env();
   325   bool enabled = vpw->is_mailbox_enabled && !!vpw->mailbox;
   333   bool do_delivery = vpw->has_mailbox && vpw->is_mailbox_enabled &&
   326 
   334     !!vpw->directory;
   327   int r = execute("vdeliver-predeliver");
   335 
   328   if(r)
   336   if (run_predeliver) {
   329     exit_msg("Execution of vdeliver-predeliver failed", r);
   337     int r = execute("vdeliver-predeliver");
   330 
   338     if(r)
   331   if(enabled) {
   339       if(r == 99)
   332     maildir = vpw->mailbox;
   340 	return 99;
       
   341       else
       
   342 	exit_msg("Execution of vdeliver-predeliver failed", r);
       
   343   }
       
   344 
       
   345   if(do_delivery) {
       
   346     maildir = vpw->directory;
   333     deliver_partial();
   347     deliver_partial();
   334   }
   348   }
   335   if(!!vpw->forwards)
   349   if(!!vpw->forwards)
   336     enqueue(vpw->forwards, host, sender);
   350     enqueue(vpw->forwards, host, sender);
   337   if(enabled)
   351   if(do_delivery)
   338     deliver_final();
   352     deliver_final();
   339 
   353 
   340   if(!fin.rewind()) {
   354   if(!fin.rewind()) {
   341     if(!o_quiet)
   355     if(!o_quiet)
   342       fout << "Could not re-rewind standard input" << endl;
   356       fout << "Could not re-rewind standard input" << endl;
   343   }
   357   }
   344   else if(execute("vdeliver-postdeliver"))
   358   else {
   345     if(!o_quiet)
   359     if (run_postdeliver) {
   346       fout << "Execution of vdeliver-postdeliver failed" << endl;
   360       int r = execute("vdeliver-postdeliver");
   347 
   361       if(r && r != 99)
       
   362 	if(!o_quiet)
       
   363 	  fout << "Execution of vdeliver-postdeliver failed" << endl;
       
   364       return r;
       
   365     }
       
   366   }
       
   367   
   348   return 0;
   368   return 0;
   349 }
   369 }