1: #!/usr/bin/perl -w
   2: 
   3: #                       # [] = optional, {} = parameter value
   4: #Usage:   perl pw.pl                #uses all DFalts
   5: #   pw.pl [-p]{permlevl}            # new nobody session with permlevel perms
   6: #   pw.pl -r{readlevl} -i{inselevl} -d{delelevl} -u{username} -t{minutes[.seconds]}
   7: #           -t is timelimit until id expires
   8: #   pw.pl [-c]{seshunid}            #checkin id and renew timelimit
   9: #           -c returns 0 if id didn't exist, -1 if expired, -2 if past 4*-t
  10: #   pw.pl -D{seshunid}              #Deletes id & ret 0 if id not exist, else 1
  11: #       (perm|read|inse|dele)levl must be an int with < 8 digits if unprefixed
  12: #       seshunid must be an 8 digit hex number regardless of (-c|-D)
  13: #   pw.pl -C  will clean out all session files which have expired so the dir
  14: #       doesn't become too cluttered.  This should probably be a monthly || weekly cron.
  15: 
  16: #sec min hour mday mon year wday yday isdst -> year yday hour min sec
  17: my $logd = "seshfilz/";  my @nowt = localtime;
  18: my @time = ($nowt[5], $nowt[7], $nowt[2], $nowt[1], $nowt[0]);
  19: my $user = "nobody"; my $sess = $news = $rest = "";
  20: my $rndm = $read = $inse = $dele = $tlim = 0;
  21: my $test = 2;
  22: 
  23: while (!$sess) {
  24:     $rndm = int rand(16**4);
  25:     $sess = sprintf "%lx", $rndm;
  26:     $rndm = int rand(16**4);
  27:     $sess .= sprintf "%lx", $rndm;
  28:     while (length($sess) < 8) { $sess = "0" . $sess; }
  29:     $news = $sess;
  30:     $sess = "$logd$sess.txt";
  31:     if (-e $sess) {
  32:         open CHEK, "<$sess";
  33:         chomp (@poop = <CHEK>);
  34:         close CHEK;
  35:         if (defined $poop[5]) {
  36:             @oldt = split /\s/, $poop[5];
  37:             if (($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] == $time[3] && $oldt[4] < $time[4]) ||
  38:                 ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] < $time[3]) ||
  39:                 ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] < $time[2]) ||
  40:                 ($oldt[0] == $time[0] && $oldt[1] < $time[1]) ||
  41:                 ($oldt[0] < $time[0])) {
  42:                 unlink "$sess";  #if rand id collides, check if collision sess
  43:             } else { $sess = ""; } # is expired and if so, use it, else new rand
  44:         }
  45:     }
  46: }
  47: foreach (@ARGV) {
  48:     if (m/^-(.)(.*)/) {
  49:         if ($1 eq "p") { $read = $inse = $dele = $2; }
  50:         elsif ($1 eq "r") { $read = $2; }
  51:         elsif ($1 eq "i") { $inse = $2; }
  52:         elsif ($1 eq "d") { $dele = $2; }
  53:         elsif ($1 eq "u") { $user = $2; }
  54:         elsif ($1 eq "t") { 
  55:             $tlim = $2;
  56:             if ($tlim =~ /(\d+)\.(\d+)/) { $time[3] += $1; $time[4] += $2; }
  57:             else { $time[3] += $tlim; }
  58:             if ($time[4] > 59) { $time[3] += int $time[4]/60; $time[4] %= 60; }
  59:             if ($time[3] > 59) { $time[2] += int $time[3]/60; $time[3] %= 60; }
  60:             if ($time[2] > 23) { $time[1] += int $time[2]/24; $time[2] %= 24; }
  61:             if ($time[1] > 365) { $time[0] += int $time[1]/365; $time[1] %= 365; }
  62:         } elsif ($1 eq "D") {
  63:             $news = $2;
  64:             $sess = "$logd$news.txt";
  65:             $test = 1;
  66:             if (!-e $sess) { $test = 0; }
  67:             else { unlink "$sess"; }
  68:         } elsif ($1 eq "c") {
  69:             $news = $2;
  70:             $sess = "$logd$news.txt";
  71:             if (!-e $sess) { $test = 0; }
  72:             else {
  73:                 open POOP, "<$sess";
  74:                 chomp (@oldf = <POOP>);
  75:                 close POOP;
  76:                 ($read, $inse, $dele, $user, $tlim) = @oldf[0..4];
  77:                 if (defined $oldf[5]) {
  78:                     @oldt = split /\s/, $oldf[5];
  79:                     if (($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] == $time[3] && $oldt[4] < $time[4]) ||
  80:                         ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] < $time[3]) ||
  81:                         ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] < $time[2]) ||
  82:                         ($oldt[0] == $time[0] && $oldt[1] < $time[1]) ||
  83:                         ($oldt[0] < $time[0])) {
  84:                         $test = -1; unlink "$sess";
  85: if ($user eq "super") { $test = -2; }
  86: # this is just a hack so no delay for super
  87:                         if ($tlim =~ /(\d+)\.(\d+)/) { $oldt[3] += 3 * $1; $oldt[4] += 3 * $2; }
  88:                         else { $oldt[3] += 3 * $tlim; }
  89:                         if ($oldt[4] > 59) { $oldt[3] += int $oldt[4]/60; $oldt[4] %= 60; }
  90:                         if ($oldt[3] > 59) { $oldt[2] += int $oldt[3]/60; $oldt[3] %= 60; }
  91:                         if ($oldt[2] > 23) { $oldt[1] += int $oldt[2]/24; $oldt[2] %= 24; }
  92:                         if ($oldt[1] > 365) { $oldt[0] += int $oldt[1]/365; $oldt[1] %= 365; }
  93:                         if (($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] == $time[3] && $oldt[4] < $time[4]) ||
  94:                             ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] < $time[3]) ||
  95:                             ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] < $time[2]) ||
  96:                             ($oldt[0] == $time[0] && $oldt[1] < $time[1]) ||
  97:                             ($oldt[0] < $time[0])) {
  98:                             $test = -2;
  99:                         }
 100:                     }
 101:                 }
 102:                 if ($tlim =~ /(\d+)\.(\d+)/) { $time[3] += $1; $time[4] += $2; }
 103:                 else { $time[3] += $tlim; }
 104:                 if ($time[4] > 59) { $time[3] += int $time[4]/60; $time[4] %= 60; }
 105:                 if ($time[3] > 59) { $time[2] += int $time[3]/60; $time[3] %= 60; }
 106:                 if ($time[2] > 23) { $time[1] += int $time[2]/24; $time[2] %= 24; }
 107:                 if ($time[1] > 365) { $time[0] += int $time[1]/365; $time[1] %= 365; }
 108:             }
 109:         } elsif ($1 eq "C") {
 110:             $test = 1;
 111:             foreach $sfil (glob("$logd*")) {
 112:                 open CHEK, "<$sfil";
 113:                 chomp (@poop = <CHEK>);
 114:                 close CHEK;
 115:                 if (defined $poop[5]) {
 116:                     @oldt = split /\s/, $poop[5];
 117:                     if (($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] == $time[3] && $oldt[4] < $time[4]) ||
 118:                         ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] == $time[2] && $oldt[3] < $time[3]) ||
 119:                         ($oldt[0] == $time[0] && $oldt[1] == $time[1] && $oldt[2] < $time[2]) ||
 120:                         ($oldt[0] == $time[0] && $oldt[1] < $time[1]) ||
 121:                         ($oldt[0] < $time[0])) {
 122:                         unlink "$sfil";
 123:                     }
 124:                 } else { unlink "$sfil"; } #this wipes ones with no limit too!
 125:             }
 126:         }
 127:     } else {
 128:         if (length($_) < 8 && $_ !~ /\D/) { #too lil2b id and has all digits
 129:             $read = $inse = $dele = $_;
 130:         } elsif (length($_) == 8) { #checkin session id as only
 131:             $news = $_;             #  unprefixed 8 digit param
 132:             $sess = "$logd$news.txt";
 133:             if (!-e $sess) { $test = 0; }
 134:             else {
 135:                 open POOP, "<$sess";
 136:                 chomp (@oldf = <POOP>);
 137:                 close POOP;
 138:                 ($read, $inse, $dele, $user, $tlim) = @oldf[0..4];
 139:                 if (defined $oldf[5]) {
 140:                     @oldt = split /\s/, $oldf[5];
 141:                     if ($oldt[0] <= $time[0] && $oldt[1] <= $time[1] && $oldt[2] <= $time[2] && $oldt[3] <= $time[3] && $oldt[4] <= $time[4]) {
 142:                         unlink "$sess"; $test = -1;
 143:                     }
 144:                 }
 145:                 if ($tlim =~ /(\d+)\.(\d+)/) { $time[3] += $1; $time[4] += $2; }
 146:                 else { $time[3] += $tlim; }
 147:                 if ($time[4] > 59) { $time[3] += int $time[4]/60; $time[4] %= 60; }
 148:                 if ($time[3] > 59) { $time[2] += int $time[3]/60; $time[3] %= 60; }
 149:                 if ($time[2] > 23) { $time[1] += int $time[2]/24; $time[2] %= 24; }
 150:                 if ($time[1] > 365) { $time[0] += int $time[1]/365; $time[1] %= 365; }
 151:             }
 152:         } else {
 153:             $rest .= "$_ "; #anything not matched above is swallowed into rest
 154:         }
 155:     }
 156: }
 157: if ($tlim eq "0") { @time = (); }
 158: if ($test eq "2") {
 159:     open SFIL, ">$sess";
 160:     print SFIL "$read\n$inse\n$dele\n$user\n$tlim\n@time\n$rest";
 161:     close SFIL;
 162:     print "$news $read $inse $dele $user $tlim @time";
 163: } else { print "$test"; } #1=success, 0=!-e, -1=tlim expired, -2=4*tlim expired