#!/usr/local/bin/perl
#  This is version 0.3 of absent
#
#               Copyright (c) 1999 by AT&T.
#  Permission to use, copy, and modify this software without fee is
#  hereby granted, provided that this entire notice is included in all
#  copies of any software which is or includes a copy or modification
#  of this software and in all copies of the supporting documentation
#  for such software.
#
#  SOME PARTS OF ABSENT MAY BE RESTRICTED UNDER UNITED STATES EXPORT
#  REGULATIONS.
#
#  THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
#  IMPLIED WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE
#  ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
#  MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
#  PURPOSE.
#       
# Author          : Christian Gilmore
# Created On      : Tue Aug  4 12:25:35 EDT 1998
# Status          : Functional
# 
# PURPOSE
#       Initializes a user into the absent opie database
# 
###############################################################################

# Location of non-standard libraries and included files
use lib qw(/usr/local/pushweb/lib
	   /usr/local/pushweb/lib/Otp
	   /usr/local/pushweb/lib/Otp/blib/arch
	  );


# Required libraries
use CGI;
use Otp;
use Fcntl;
use NDBM_File;
use Response_Pages;


# Environment Variables
$ENV{'PATH'} = "/usr/local/bin:$ENV{'PATH'}";


# Globals
$ETCDIR = "/usr/local/pushweb/etc";
$USERDIR = $ETCDIR . "/users";
# file storing personal home pages.  must be writable by whomever is
# running this script.
$PERSONALPAGEFILE = $USERDIR . "/personal_pages";


# Print out a content-type for HTTP/1.1 compatibility
print "Content-type: text/html\n\n";

###############################################################################
###############################################################################
# Main Program
###############################################################################
###############################################################################
{
  print $Response_Pages::RegistrationHeader;

  my $query = new CGI;

  my $userid = $query->param('userid');
  my $passwd = $query->param('passwd');
  my $passphrase = $query->param('passphrase');
  my $passphrase2 = $query->param('passphrase2');
  my $personalpage = $query->param('personalpage');
  my %personalpagehash = ();

  if ($userid =~ /[^\w_]/) {
    print "<p>\n<strong>Error</strong>: Userids can contain only ";
    print "alphanumerics and underscores.</p>\n";
    print $Response_Pages::RegistrationFooter;
    exit(0);
  }

  my ($kuserid, $kpasswd, @trash) = getpwnam($userid);
  my $encpasswd = crypt($passwd, $kpasswd);
  my $passlength = length($passphrase);

 SWITCH: {
    if ($userid ne $kuserid || $userid eq '') {
      print "<p>\n<strong>Error</strong>: User $userid not found.</p>\n";
      last SWITCH;
    }

    if ($encpasswd ne $kpasswd) {
      print "<p>\n<strong>Error</strong>: Account password incorrect.</p>\n";
      last SWITCH;
    }
    
    if ($passphrase ne $passphrase2) {
      print "<p>\n<strong>Error</strong>: Passphrases do not match.</p>\n";
      last SWITCH;
    }
    
    if ($passlength < 10 || $passlength > 127) {
      print "<p>\n<strong>Error</strong>: Passphrase must be between 10\n";
      print "and 127 characters.</p>\n";
      last SWITCH;
    }

    my $result = Otp::register_user($userid, $passphrase, 0);

    if ($result != 1) {
      print "<p>\n<strong>Error</strong>: There was an error during OTP\n";
      print "registration. Contact your absent administrator.</p>\n";
      last SWITCH;
    }

    if ($personalpage) {
      &lockdb("$PERSONALPAGEFILE.lock");
      tie(%personalpagehash, NDBM_File, $PERSONALPAGEFILE, 
	  O_RDWR|O_CREAT, 0664);
      $personalpagehash{$userid} = $personalpage;
      untie(%personalpagehash);
      &unlockdb("$PERSONALPAGEFILE.lock");
    } elsif (exists $personalpagehash{$userid}) {
      &lockdb("$PERSONALPAGEFILE.lock");
      tie(%personalpagehash, NDBM_File, $PERSONALPAGEFILE, 
	  O_RDWR|O_CREAT, 0664);
      delete $personalpagehash{$userid};
      untie(%personalpagehash);
      &unlockdb("$PERSONALPAGEFILE.lock");
    }
    
    print "<p>\nYour passphrase has been set. You are now able to use\n";
    print "the <strong>absent</strong> system. Please be sure that your\n";
    print "first OTP number is 498. If it is not, please <a\n";
    print "href=\"mailto:absent\@research.att.com\">let us know</a>.\n</p>\n";
  }

  print $Response_Pages::RegistrationFooter;
}

###############################################################################
###############################################################################
# Lock the database
###############################################################################
###############################################################################
sub lockdb {
  my $lock = shift;
  my $border = shift;
  my $timeout = 20 + time;

  while (-e $lock && time < $timeout) {
    sleep(1);
  }

  open(LOCK, ">$lock") || &file_open_error($lock, $border);
}

###############################################################################
###############################################################################
#  Unlock the database
###############################################################################
###############################################################################
sub unlockdb {
  my $lock = shift;

  close(LOCK);
  unlink($lock);
}

###############################################################################
###############################################################################
#  Report an error on file opening
###############################################################################
###############################################################################
sub file_open_error {
  my $errfile = shift;

  print "<p>\nError: Unable to obtain a lock for $errfile. ";
  print "Please try again.\n</p>\n\n";

  print $Response_Pages::RegistrationFooter;
  exit(2);
}

# $Id: absent_init.pl,v 1.1 1999/12/17 00:02:41 cgilmore Exp $

# $Log: absent_init.pl,v $
# Revision 1.1  1999/12/17 00:02:41  cgilmore
# Initial revision
#
