#!/usr/bin/perl -w
#
# $Id: unix_verlaengern.pl,v 1.1 1997/06/04 21:32:21 webadm Exp $
#
#
# DESCRIPTION: unix_verlaengern.pl - display all users in a WWW-Formular and
#    forward choosen users for further processing to benutzerantrag_senden.pl
#
# URL: none yet
#
# AUTHOR: Cord Beermann (cord@Wunder-Nett.org)
#
# Thanks to: Ralf Begemann (begemann@cc.fh-lippe.de)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# A Perl script is "correct" if it gets the job done before your boss fires
# you.
# ('Programming Perl Second Edition' by Larry Wall, Tom Christiansen
#   & Randal. L. Schwartz)
#
# Bugs and shortcomings
# none yet
#
# Todo
# * write instructions
#
### Configuration ###
#
# where we should store that little user.u files ;-)
$DIR='/var/infosystems/www/security/login';
#
# which chars do we want as indices
@index= qw(a b c d e f g h i j k l m n o pq r s t uv w xyz);
#
### End of Configuration ###

# include perl-modules
use CGI::Carp;
use CGI qw(:standard);
use CGI qw(:shortcuts);

# initialize the CGI-Module and load the form-input
$query = new CGI;
$query->autoEscape(undef);

# print HTTP and HTML-Header
print $query->header;
print $query->start_html(-"title"=>'Benutzerzugang f&uuml;r UNIX verl&auml;ngern',
                         -"author"=>'gnats@thalassa.cc.fh-lippe.de',
                         -"base"=>'true',
                         -"text"=>'#000000',
                         -"BGCOLOR"=>'#FFFFF7');
print "\n";

print(h1("Benutzerzugang f&uuml;r UNIX verl&auml;ngern"),
      hr
      );

# check if we got some input to process
if ($query->query_string =~ /=on/) {
    # loop on each parameter we got.
    foreach $name ($query->param) {
	if ($query->param($name) eq 'on') {
	    # touch the file user.u
	    open(LOGIN, '>' . $DIR . '/' . $name . '.u') or
		die("open $DIR/$name.: $!");
	    close(LOGIN);
	    # give a receipt back
	    print "<p>Verl&auml;ngerung f&uuml;r <em>$name</em> weitergeleitet</p>\n";
	}
    }
    # add the end add a link back to the userlist
    print hr, "\n", $query->a({href=>$query->url},'Hauptseite');
} else {
    # start the WWW-Form
    print $query->startform();
    # print the leading index, to jump in the list
    foreach $name (@index) {
	print "[<A HREF=\"#$name\">$name</a>]";
    }
    # print a button, to submit the choosen users
    print p($query->submit(-name=>'Verl&auml;ngern')), hr;

    # get the nis-database, sort it and loop over each line
    foreach (sort (`/usr/bin/niscat passwd.org_dir.cc.fh-lippe.de`)) {
	# get the relevant parts from the line
	($login,$realname,$shell) = (split(/:/))[0,4,6];
	if ($shell eq '/opt/local/bin/gesperrt') {
	    # if we find a disabled account, print a special message
	    push(@list, "$login:$realname:ZUGANG GESPERRT<br>");
	} else {
	    # else print login, realname and a checkbox
	    push(@list, "$login:$realname",
		 $query->checkbox(-name=>$login,
				  -label=>''
				  ),
		 "<br>\n"
		 );
	}
    }
    foreach $head (@index) {
	# print a jumpmark for the index.
	print "<H2><A NAME=\"$head\">$head</a></H2>\n";
	while ($name = shift(@list)) {
	    if (substr($name,0,1) le substr($head,-1,1)) {
		print $name;
	    } else {
		unshift(@list,$name);
		last;
	    }
	}
    }

    # print another button for submitting.
    print hr, $query->submit(-name=>'Verl&auml;ngern');
}

print(hr, address('Autor: ' . a({href=>'http://Cord.de/'}, 'Cord Beermann'
				)
		  )
      );

