Initial commit
[clearscm.git] / maps / bin / MAPSDeliver
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: MAPSDeliver,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  This script simply delivers the mail. It is separated out so
7 #               it can be the only portion that is setgid to the group mail
8 #               for the purposes of being able to deliver the mail to the users
9 #               maildrop
10 # Author:       Andrew@DeFaria.com
11 # Created:      Fri Nov 29 14:17:21  2002
12 # Modified:     $Date: 2013/06/12 14:05:47 $
13 # Language:     perl
14 #
15 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
16 #
17 ################################################################################
18 use strict;
19 use warnings;
20
21 use English;
22 use FindBin;
23
24 # Untaint $FindBin::Bin
25 my $lib;
26
27 BEGIN {
28   if ($FindBin::Bin =~ /^(.*)$/) {
29     $lib = $1;
30   } # if
31 } # BEGIN
32
33 use lib $lib;
34
35 use MAPSFile;
36 use MAPSDB;
37 use MAPSLog;
38
39 sub DeliverMail ($$) {
40   my ($userid, $msgfileName) = @_;
41
42   # Switch to group mail
43   $EGID = getgrnam "mail";
44
45   # Untaint $userid
46   if ($userid =~ /^([-\@\w.]+)$/) {
47     $userid = $1;
48   } # if
49
50   # Open maildrop file
51   open my $maildrop, '>>', "/var/mail/$userid"
52     or return "Unable to open maildrop file (/var/mail/$userid): $!";
53
54   # Open msgfile
55   open my $msgfile, '<', $msgfileName
56     or return "Unable to open msgfile ($msgfileName): $!";
57
58   # Lock file
59   Lock $maildrop;
60
61   # Write msgfile -> $maildrop
62   print $maildrop $_
63     while (<$msgfile>);
64
65   # Unlock the file
66   Unlock $maildrop;
67
68   # Close files
69   close $maildrop;
70   close $msgfile;
71
72   return;
73 } # DeliverMail
74
75 # Main
76 die 'User id not specified' unless $ARGV [0];
77 die 'Msgfile not specified' unless $ARGV [1];
78
79 my $userid  = shift @ARGV;
80 my $msgfile = shift @ARGV;
81
82 my $err  = DeliverMail $userid, $msgfile;
83
84 if ($err) {
85   OpenDB 'mapsadmin', 'mapsadmin';
86
87   MAPSDB::SetContext $userid;
88
89   Error $err;
90 } # if
91
92 exit 1 if $err;
93 exit 0;