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