Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'd like to create an aliased mail account, ported to a perl script, to prepend a file with the new message. I've gotten it to work fine as the user I defined for this task, and as root, but I get setgid and setuid problems when I have the mail user run the script.

I'm new at Linux/Unix programming, so I studied some of Mailman (yeah, Python) and Majordomo, and discovered they both use a wrapper program written in 'C' to accomplish this. Hmmm.

So, can it be done with Perl? Can anyone point me in the right direction?

Regards,
Rob Martin

Replies are listed 'Best First'.
Re: Mail/CGI Alias
by httptech (Chaplain) on Apr 24, 2000 at 15:04 UTC
    I may be missing something here, but it sounds like all you need to do is set the group of the file being appended to the same group the mail daemon is running as, and then set the file group writable.

    For example, if the daemon group was 'nobody', your file would look like:

    -rw-rw--- 1 you nobody 0 Apr 1 2000 append.txt
Re: Mail/CGI Alias
by BBQ (Curate) on Apr 24, 2000 at 18:00 UTC
    > So, can it be done with Perl?
    Anything can be done with perl... even toast.

    #!/home/bbq/bin/perl
    # Trust no1!
Re: Mail/CGI Alias
by ZZamboni (Curate) on Apr 24, 2000 at 17:52 UTC
    It would help if you include the non-working code that you have so far, with descriptions of error messages that you get.

    If all you need is appending messages to a file, you don't need Perl to do that. A simple mail alias defined in /etc/aliases (or the corresponding NIS/NIS+ table, if you use that) or in the user's .forward file will suffice, using the following format:

    "|/bin/cat > /file/to/append.txt"
    or simply
    "/file/to/append.txt"
    Remember that if the alias is defined in /etc/aliases or other system table, the program gets executed as root (daemon in some systems?), so you need to make sure the file has correct permissions. You could also pipe it to a setuid/setgid program, and then make sure the file again has appropriate permissions.

    If you need to do some pre-processing on the message before appending it, then you need to pipe it to a program, and that is where Perl could be useful. Take a look at the Mail::Internet package for mail manipulation utilities. Assuming your program leaves the (possibly) modified message on STDOUT, you need an alias like this:

    "|/your/program.pl > /file/to/append.txt"
    in which case again the file gets written by root/daemon. If your program open the file for writing internally, then you only need
    "|/your/program.pl"
    and make sure the setuid/setgid settings of your program match the permissions on the file.
RE: Mail/CGI Alias
by Aighearach (Initiate) on Apr 24, 2000 at 09:52 UTC
    What exactly is the setuig and setgid problems you are having? Yes, this can certainly be done in Perl. All things can be done in perl. ;) Just show us your code, or your error log, and we will assist you.
    --4c6966653a205468652073656172636820666f
      7220746861742070657266656374204765656b
      20436869632c20746865206f6e652077697468
      2074686520737061726b6c696e672065796573
      2077686f20706c617973206973207261696e2e
    
      Though this isn't a perl solution, you might want to look into procmail. It allows you to do nearly all things with ingoing and outgoing mail. (The typical ingoing method is providing a pipe in .forward.)