#!/usr/bin/perl
#
# Copyright (c) 2003 Stefan Moeding <s.moeding@setuid.de>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#       $Id$
#

##############################################################################
#
# Sicheres Lesen oder Schreiben in Userverzeichnissen
#
# Diese Programm ermöglicht das Lesen oder Schreiben einer Datei mit den
# Rechten eines vorgegeben Users. Die nachfolgenden Optionen sind möglich:
#
# --user        Usernamen, der zum Öffnen der Files benutzt wird.
# --group       Gruppe, die zum Öffnen der Files benutzt wird. Fehlt dieser
#               Parameter, dann wird die primäre Gruppe des mit --user
#               gesetzten Users genommen
# --umask       Umask, die beim Schreiben eines neuen Files verwendet wird.
# --input       Name der Eingabedatei.
# --output      Name der Ausgabedatei.
# --append      Flag zum Anhängen an die Ausgabedatei.
#
# Beispiele:
#
# echo "Fehlermeldung" | secat --user=xyz00 --output=err.log --append
#
# Ausgabe der Fehlermeldung in die Datei 'err.log'. Diese wird als User
# xyz00 geöffnet und es kommt eine Fehlermeldung, falls dieser Benutzer
# die Datei nicht beschreiben darf.
#
# secat --user=xyz00 --input=passwd >/tmp/passwd
#
# Lesen der Datei passwd mit den Rechten des Users xyz00. Da die I/O-
# Umleitung von der aufrufenden Shell gemacht wird, gehört die Datei
# /tmp/passwd anschließend dem aufrufenden Benutzer.
#
# secat --user=xyz00 --group=httpd --input=log --output=old --umask=027
#
# Kopieren einer Datei in einem Userverzeichnis ohne dabei root-Rechte
# anzuwenden. Die Quelldatei muß von xyz00 lesbar sein und die Zieldatei
# gehört anschließend dem User xyz00 und der Gruppe httpd. Die Rechte
# verbieten ein Schreiben für die Gruppe und jeden Zugriff für die Welt.


use strict;
use Getopt::Long;


##############################################################################
#
# Options
#
my %opt = ();

GetOptions(\%opt,
	   "user=s", "group=s", "umask=s",
	   "input=s", "output=s", "append!") ||
  die("$0: Illegal argument\n");


##############################################################################
#
# Userkontext (UID und GID) wechseln
# (nur möglich, wenn das Script als root aufgerufen wird).
#

if (defined($opt{"user"})) {
  my ($name, $passwd, $uid, $gid, $rest) = getpwnam($opt{"user"});

  die("$0: UID for ".$opt{"user"}." not found") if (!defined($uid));

  if (defined($opt{"group"})) {
    ($name, $passwd, $gid, $rest) = getgrnam($opt{"group"});

    die("$0: GID for ".$opt{"group"}." not found") if (!defined($gid));
  }

  $) = $gid." ".$gid;
  $( = $gid;

  $< = $uid;
  $> = $uid;
}


##############################################################################
#
# umask anpassen falls gesetzt.
#

umask oct($opt{"umask"}) if (defined($opt{"umask"}));


##############################################################################
#
# Filename für Ein- und Ausgabe setzen.  Falls keine Files als Parameter
# gesetzt wurden, wird '-' für STDIN bzw. STDOUT gesetzt.  Für die Ausgabe
# wird der Mode um '>' erweitert, falls an die Datei angehängt werden soll.
#

my $input  = (defined($opt{"input"}))  ? $opt{"input"}  : "-";
my $output = (defined($opt{"output"})) ? $opt{"output"} : "-";

my $append = ($opt{"append"} == 1) ? ">" : "";


##############################################################################
#
# Dateien öffnen und vom Inputfile zum Outputfile kopieren.
#

open(IN,  "<".$input) || die("$input: $!\n");
open(OUT, ">".$append.$output) || die("$output: $!\n");

while(<IN>) {
  print OUT;
}

close(IN);
close(OUT);

exit 0;
