| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
## Scott Wiersdorf |
| 5 |
## Created: Mon May 19 11:55:06 MDT 2003 |
| 6 |
## $Id$ |
| 7 |
|
| 8 |
## a simple CGI for crontab editing |
| 9 |
## |
| 10 |
## most useful under Apache's suEXEC wrapper ;o) |
| 11 |
## |
| 12 |
## This program is a demonstration of the capabilities of the |
| 13 |
## Config::Crontab module. The bulk of this program is CGI logic (tip |
| 14 |
## o' the hat to Lincoln Stein), but look for the Config::Crontab::* |
| 15 |
## calls and objects named $ct, $block, $newblock, and $obj. These are |
| 16 |
## all Config::Crontab::* objects (and the reason for this demo |
| 17 |
## program's creation). |
| 18 |
## |
| 19 |
|
| 20 |
## |
| 21 |
## WARNING * ACHTUNG * AVISO ## |
| 22 |
## |
| 23 |
## This program may modify your crontab file. This program is provided |
| 24 |
## AS IS under the terms of the Perl Artistic License to illustrate |
| 25 |
## some possible uses of Config::Crontab. |
| 26 |
## |
| 27 |
## This program inherently is a security problem because it provides |
| 28 |
## no authentication mechanism. You should enable your web server's |
| 29 |
## authentication to prevent unauthorized access to this script. |
| 30 |
## |
| 31 |
## The author assumes no liability for damages of any kind caused by |
| 32 |
## (mis)?use of this program. Please see paragraph 10 of the Artistic |
| 33 |
## License distributed with Perl. Have a nice day! |
| 34 |
## |
| 35 |
|
| 36 |
## to do: |
| 37 |
## |
| 38 |
## - enable/disable blocks and lines within a block |
| 39 |
|
| 40 |
use CGI; |
| 41 |
use Config::Crontab; |
| 42 |
|
| 43 |
######################### |
| 44 |
## user-servicable parts: |
| 45 |
######################### |
| 46 |
my $file = ''; ## set this to a pathname of a file (it needn't |
| 47 |
## exist) if you want to practice with a temporary |
| 48 |
## crontab file instead of your real one |
| 49 |
my $DEBUG = 0; ## a little extra information in your error_log |
| 50 |
|
| 51 |
####################################### |
| 52 |
## no user-servicable parts below here: |
| 53 |
####################################### |
| 54 |
my $q = new CGI; |
| 55 |
my $self = $q->url; |
| 56 |
my $info; ## used for passing messages to main form |
| 57 |
|
| 58 |
print $q->header; |
| 59 |
|
| 60 |
ACTION: { |
| 61 |
|
| 62 |
############################################################# |
| 63 |
############################################################# |
| 64 |
## ## |
| 65 |
## General Block Operations: these don't require 'blockno' ## |
| 66 |
## ## |
| 67 |
############################################################# |
| 68 |
############################################################# |
| 69 |
|
| 70 |
############################################## |
| 71 |
## Cancel Block Operation ## |
| 72 |
############################################## |
| 73 |
if( $q->param('Block Cancel') || $q->param('Cancel') ) { |
| 74 |
$info = "Action cancelled"; |
| 75 |
last ACTION; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
############################################## |
| 80 |
## Complete Block Operation ## |
| 81 |
############################################## |
| 82 |
elsif( $q->param('Block Done') ) { |
| 83 |
$info = "Action completed"; |
| 84 |
last ACTION; |
| 85 |
} |
| 86 |
|
| 87 |
############################################## |
| 88 |
## New Raw Block ## |
| 89 |
############################################## |
| 90 |
elsif( $q->param('Block Raw New') ) { |
| 91 |
## display edit page and exit |
| 92 |
print $q->start_html("New crontab block for " . getpwuid($<)), |
| 93 |
$q->strong('Add a new crontab entry:'), $q->p, "\n\n"; |
| 94 |
print "<ul>" . $info . "</ul>" if $info; |
| 95 |
print $q->start_form; |
| 96 |
print $q->textarea( -name => 'blocktext', |
| 97 |
-default => '', |
| 98 |
-rows => 10, |
| 99 |
-columns => 50 ); |
| 100 |
print $q->p, "\n"; |
| 101 |
print $q->submit('Block New Commit', 'Commit'), $q->reset, $q->submit('Block Cancel', 'Cancel'); |
| 102 |
print $q->end_form; |
| 103 |
print $q->end_html; |
| 104 |
exit; |
| 105 |
} |
| 106 |
|
| 107 |
########################################## |
| 108 |
## New Block Commit ## |
| 109 |
########################################## |
| 110 |
elsif( $q->param('Block New Commit') ) { |
| 111 |
my $ct = new Config::Crontab( -file => $file ); $ct->read; |
| 112 |
my $bt = $q->param('blocktext'); $bt =~ s/\r\n/\n/g; |
| 113 |
my $newblock = new Config::Crontab::Block( -data => $bt ); |
| 114 |
$ct->last($newblock); |
| 115 |
$ct->write; |
| 116 |
$info = "New block added"; |
| 117 |
last ACTION; |
| 118 |
} |
| 119 |
|
| 120 |
############################################## |
| 121 |
## New Block ## |
| 122 |
############################################## |
| 123 |
elsif( $q->param('Block New') ) { |
| 124 |
my $ct = new Config::Crontab( -file => $file ); $ct->read; |
| 125 |
$ct->first(new Config::Crontab::Block(-data => '## new cron event') ); |
| 126 |
$ct->write; |
| 127 |
|
| 128 |
$q->param('blockno', 0); |
| 129 |
$q->param('Block Edit', 1); |
| 130 |
} |
| 131 |
|
| 132 |
## add new non-blockno operations here |
| 133 |
|
| 134 |
######################################################### |
| 135 |
######################################################### |
| 136 |
## ## |
| 137 |
## Block Operations: these require 'blockno' to be set ## |
| 138 |
## ## |
| 139 |
######################################################### |
| 140 |
######################################################### |
| 141 |
unless( defined $q->param('blockno') ) { |
| 142 |
print STDERR "No block found. Jumping to end of ACTION\n" if $DEBUG; |
| 143 |
last ACTION; |
| 144 |
} |
| 145 |
|
| 146 |
## parse the crontab file |
| 147 |
my $ct = new Config::Crontab( -file => $file ); $ct->read; |
| 148 |
my $block = ($ct->blocks)[$q->param('blockno')]; |
| 149 |
unless( ref($block) ) { |
| 150 |
$info = "Couldn't find block!"; |
| 151 |
print STDERR "Block " . $q->param('blockno') . " missing\n" if $DEBUG; |
| 152 |
last ACTION; |
| 153 |
} |
| 154 |
|
| 155 |
############################################## |
| 156 |
## Raw Block Edit ## |
| 157 |
############################################## |
| 158 |
if( $q->param('Block Raw Edit') ) { |
| 159 |
print $q->start_html("Edit crontab block for " . getpwuid($<)), |
| 160 |
$q->strong('Edit this block:'), $q->p, "\n\n"; |
| 161 |
print "<ul>" . $info . "</ul>" if $info; |
| 162 |
print $q->start_form; |
| 163 |
print $q->hidden('blockno', $q->param('blockno')); |
| 164 |
print $q->textarea( -name => 'blocktext', |
| 165 |
-default => $block->dump, |
| 166 |
-rows => 10, |
| 167 |
-columns => 50 ); |
| 168 |
print $q->p, "\n"; |
| 169 |
print $q->submit('Block Commit', 'Commit'), |
| 170 |
$q->reset, |
| 171 |
$q->submit('Block Cancel', 'Cancel'); |
| 172 |
print $q->end_form; |
| 173 |
print $q->end_html; |
| 174 |
exit; |
| 175 |
} |
| 176 |
|
| 177 |
############################################## |
| 178 |
## Commit Block ## |
| 179 |
############################################## |
| 180 |
elsif( $q->param('Block Commit') ) { |
| 181 |
unless( defined $q->param('blocktext') && $q->param('blocktext') ) { |
| 182 |
$info = "No blocktext"; |
| 183 |
print STDERR "Commit: $info\n" if $DEBUG; |
| 184 |
last ACTION; |
| 185 |
} |
| 186 |
|
| 187 |
my $bt = $q->param('blocktext'); $bt =~ s/\r\n/\n/g; |
| 188 |
my $newblock = new Config::Crontab::Block( -data => $bt ); |
| 189 |
$ct->replace( $block, $newblock ); |
| 190 |
$ct->write; |
| 191 |
$info = "New block written"; |
| 192 |
last ACTION; |
| 193 |
} |
| 194 |
|
| 195 |
############################################## |
| 196 |
## Delete Block ## |
| 197 |
############################################## |
| 198 |
elsif( $q->param('Block Delete') ) { |
| 199 |
$ct->remove($block); |
| 200 |
$ct->write; |
| 201 |
$info = "Block deleted!"; |
| 202 |
last ACTION; |
| 203 |
} |
| 204 |
|
| 205 |
############################################## |
| 206 |
## Move Block ## |
| 207 |
############################################## |
| 208 |
elsif( $q->param('Block Up') || $q->param('Block Down') || |
| 209 |
$q->param('Block First') || $q->param('Block Last') ) { |
| 210 |
|
| 211 |
if( $q->param('Block Up') ) { $ct->up($block) } |
| 212 |
elsif( $q->param('Block Down') ) { $ct->down($block) } |
| 213 |
elsif( $q->param('Block First') ) { $ct->first($block) } |
| 214 |
elsif( $q->param('Block Last') ) { $ct->last($block) } |
| 215 |
|
| 216 |
$ct->write; |
| 217 |
$info = "Block moved"; |
| 218 |
last ACTION; |
| 219 |
} |
| 220 |
|
| 221 |
############################################## |
| 222 |
## Edit Block ## |
| 223 |
## ## |
| 224 |
## This section is a "substate" since it ## |
| 225 |
## loops within itself until 'Done' is ## |
| 226 |
## pressed. ## |
| 227 |
## ## |
| 228 |
############################################## |
| 229 |
if( $q->param('Block Edit') ) { |
| 230 |
|
| 231 |
BLOCK_EDIT: { |
| 232 |
|
| 233 |
###################################### |
| 234 |
## all sections below require 'blockno' |
| 235 |
###################################### |
| 236 |
my $block = ($ct->blocks)[$q->param('blockno')]; |
| 237 |
unless( ref($block) ) { |
| 238 |
$info = "Couldn't find block"; |
| 239 |
print STDERR "$info (" . $q->param('blockno') . ")\n" if $DEBUG; |
| 240 |
last ACTION; |
| 241 |
} |
| 242 |
|
| 243 |
###################################### |
| 244 |
## 'objno' not required |
| 245 |
###################################### |
| 246 |
|
| 247 |
if( $q->param('New Line') ) { |
| 248 |
if( $q->param('newobjtype') eq 'Comment' ) { |
| 249 |
$block->last(new Config::Crontab::Comment(-data => '## comment')); |
| 250 |
} elsif( $q->param('newobjtype') eq 'Environment' ) { |
| 251 |
$block->last(new Config::Crontab::Env(-data => 'NAME=value')); |
| 252 |
} elsif( $q->param('newobjtype') eq 'Event' ) { |
| 253 |
$block->last(new Config::Crontab::Event(-data => '0 0 * * 1 /bin/true')); |
| 254 |
} else { |
| 255 |
$info = "Unknown object type"; |
| 256 |
print STDERR "Edit (new line): $info (" . $q->param('newobjtype') . ")\n" if $DEBUG; |
| 257 |
last ACTION; |
| 258 |
} |
| 259 |
$ct->write; |
| 260 |
$info = "New " . $q->param('newobjtype') . " created"; |
| 261 |
last BLOCK_EDIT; |
| 262 |
} |
| 263 |
|
| 264 |
###################################### |
| 265 |
## the following sections require 'objno' |
| 266 |
###################################### |
| 267 |
last BLOCK_EDIT unless defined $q->param('objno'); |
| 268 |
|
| 269 |
my $line = ($block->lines)[$q->param('objno')]; |
| 270 |
|
| 271 |
if( $q->param('Delete Line') ) { |
| 272 |
$block->remove($line); |
| 273 |
$info = "Line deleted"; |
| 274 |
} |
| 275 |
elsif( $q->param('Commit Line') ) { |
| 276 |
my $obj; |
| 277 |
if( $q->param('objtype') eq 'comment' ) { |
| 278 |
$obj = new Config::Crontab::Comment( -data => $q->param('comment') ); |
| 279 |
} elsif( $q->param('objtype') eq 'env' ) { |
| 280 |
$obj = new Config::Crontab::Env( -name => $q->param('envname'), |
| 281 |
-value => $q->param('envvalue') ); |
| 282 |
} elsif( $q->param('objtype') eq 'event' ) { |
| 283 |
$obj = new Config::Crontab::Event( -datetime => $q->param('datetime'), |
| 284 |
-command => $q->param('command') ); |
| 285 |
} |
| 286 |
else { |
| 287 |
$info = "Invalid object type: " . $q->param('objtype'); |
| 288 |
print STDERR "$info\n" if $DEBUG; |
| 289 |
last ACTION; |
| 290 |
} |
| 291 |
$block->replace($line, $obj); |
| 292 |
$info = "Line changes saved"; |
| 293 |
} |
| 294 |
elsif( $q->param('Revert Line') ) { |
| 295 |
$info = "Line reverted"; |
| 296 |
} |
| 297 |
elsif( $q->param('First Line') ) { |
| 298 |
$block->first($line); |
| 299 |
$info = "Line moved to first"; |
| 300 |
} |
| 301 |
elsif( $q->param('Last Line') ) { |
| 302 |
$block->last($line); |
| 303 |
$info = "Line moved to last"; |
| 304 |
} |
| 305 |
elsif( $q->param('Up Line') ) { |
| 306 |
$block->up($line); |
| 307 |
$info = "Line moved up"; |
| 308 |
} |
| 309 |
elsif( $q->param('Down Line') ) { |
| 310 |
$block->down($line); |
| 311 |
$info = "Line moved down"; |
| 312 |
} |
| 313 |
else { last BLOCK_EDIT; } |
| 314 |
$ct->write; |
| 315 |
last BLOCK_EDIT; |
| 316 |
} |
| 317 |
|
| 318 |
## this will trigger if user deletes last line in a block, or the |
| 319 |
## block was emptied by someone else before we got here (no locking) |
| 320 |
last ACTION unless $block->lines; |
| 321 |
|
| 322 |
## display edit block page and exit |
| 323 |
print $q->start_html("Edit crontab block for " . getpwuid($<)), |
| 324 |
$q->strong('Edit this block:'), "<br>\n"; |
| 325 |
print "Date: " . scalar(localtime) . "<br>\n"; |
| 326 |
print "<ul>" . $info . "</ul>" if $info; |
| 327 |
|
| 328 |
print $q->start_form; |
| 329 |
$q->param('Block Edit', 0); |
| 330 |
print $q->hidden('Block Edit'); |
| 331 |
print $q->submit('Block Done', 'Done'), $q->reset; |
| 332 |
print "<br>Be sure to 'Commit' your changes before hitting 'Done'<br>\n"; |
| 333 |
print $q->end_form; |
| 334 |
|
| 335 |
print qq!<table border="1"><tr valign="top">\n!; |
| 336 |
print qq!<td>!, $q->start_form; |
| 337 |
print $q->submit("New Line", "Add"), $q->br; |
| 338 |
print $q->hidden('blockno'); |
| 339 |
$q->param('Block Edit', 1); |
| 340 |
print $q->hidden('Block Edit'); |
| 341 |
|
| 342 |
print $q->radio_group(-name => 'newobjtype', |
| 343 |
-values => ['Comment','Environment','Event'], |
| 344 |
-default => 'Event'); |
| 345 |
print "\n"; |
| 346 |
print $q->end_form, qq!</td></tr></table>\n!; |
| 347 |
|
| 348 |
print qq!<table border="1">\n!; |
| 349 |
print qq!<tr><td>Delete</td><td>Commit</td><td>Undo</td><td>First</td><td>Up</td><td>Down</td><td>Last</td></tr>\n!; |
| 350 |
my $cols = 10; |
| 351 |
my $i = 0; |
| 352 |
for my $obj ( $block->lines ) { |
| 353 |
print $q->start_form; |
| 354 |
print qq!<tr valign="top">\n!; |
| 355 |
print q!<td width="! . int(100*(1/$cols)) . q!%">!, |
| 356 |
$q->submit("Delete Line", 'X'), "</td>\n"; |
| 357 |
print q!<td width="! . int(100*(1/$cols)) . q!%">!, |
| 358 |
$q->submit("Commit Line", 'O'), "</td>\n"; |
| 359 |
print q!<td width="! . int(100*(1/$cols)) . q!%">!, |
| 360 |
$q->submit("Revert Line", 'U'), "</td>\n"; |
| 361 |
print q!<td width="! . int(100*(1/$cols)) . q!%">!, |
| 362 |
$q->submit("First Line", '|<'), "</td>\n"; |
| 363 |
print q!<td width="! . int(100*(1/$cols)) . q!%">!, |
| 364 |
$q->submit("Up Line", '<'), "</td>\n"; |
| 365 |
print q!<td width="! . int(100*(1/$cols)) . q!%">!, |
| 366 |
$q->submit("Down Line", '>'), "</td>\n"; |
| 367 |
print q!<td width="! . int(100*(1/$cols)) . q!%">!, |
| 368 |
$q->submit("Last Line", '>|'), "</td>\n"; |
| 369 |
$q->param('Block Edit', 1); |
| 370 |
print $q->hidden('Block Edit'), "\n"; |
| 371 |
$q->param('objno', $i++); |
| 372 |
print $q->hidden(-name => 'objno'), "\n"; |
| 373 |
print $q->hidden('blockno'), "\n"; |
| 374 |
|
| 375 |
if( UNIVERSAL::isa($obj, 'Config::Crontab::Comment') ) { |
| 376 |
print qq!<td colspan="3" width="! . int(100*(3/$cols)) . q!%">!; |
| 377 |
$q->param('comment', $obj->dump); |
| 378 |
print "Comment:<br>", $q->textfield(-name => "comment", |
| 379 |
-size => 25), "\n"; |
| 380 |
print $q->hidden('objtype', 'comment'), "\n"; |
| 381 |
print "</td>"; |
| 382 |
} |
| 383 |
|
| 384 |
elsif( UNIVERSAL::isa($obj, 'Config::Crontab::Env') ) { |
| 385 |
print qq!<td>!; |
| 386 |
$q->param('envname', $obj->name); |
| 387 |
print "Name:<br>", $q->textfield(-name => "envname", |
| 388 |
-size => 12), "\n"; |
| 389 |
print "</td><td>=</td><td>"; |
| 390 |
$q->param('envvalue', $obj->value); |
| 391 |
print "Value:<br>", $q->textfield(-name => "envvalue", |
| 392 |
-value => $obj->value), "\n"; |
| 393 |
print $q->hidden('objtype', 'env'), "\n"; |
| 394 |
print "</td>"; |
| 395 |
} |
| 396 |
|
| 397 |
elsif( UNIVERSAL::isa($obj, 'Config::Crontab::Event') ) { |
| 398 |
print qq!<td colspan="1" width="! . int(100*(1/$cols)) . q!%">!; |
| 399 |
$q->param('datetime', $obj->datetime); |
| 400 |
print "Scheduled:<br>", $q->textfield(-name => "datetime", |
| 401 |
-size => 12), "\n"; |
| 402 |
print qq!</td><td colspan="2" width="! . int(100*(2/$cols)) . q!%">!; |
| 403 |
$q->param('command', $obj->command); |
| 404 |
print "Command:<br>", $q->textfield(-name => "command"), "\n"; |
| 405 |
print $q->hidden('objtype', 'event'), "\n"; |
| 406 |
print "</td>"; |
| 407 |
} |
| 408 |
print "</tr>\n\n"; |
| 409 |
print $q->end_form; |
| 410 |
} |
| 411 |
|
| 412 |
print "</table>\n"; |
| 413 |
print $q->start_form; |
| 414 |
$q->param('Block Edit',0); |
| 415 |
print $q->hidden('Block Edit'); |
| 416 |
print $q->submit('Block Done', 'Done'), $q->reset; |
| 417 |
print "<br>Be sure to 'Commit' your changes before hitting 'Done'<br>\n"; |
| 418 |
print $q->end_form; |
| 419 |
print $q->end_html; |
| 420 |
|
| 421 |
exit; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
print $q->start_html("Crontab for " . getpwuid($<)), |
| 426 |
$q->strong('Edit your crontab:'), "<br>\n\n"; |
| 427 |
print "Date: " . scalar(localtime) . "<br>\n"; |
| 428 |
print "<ul><em>" . $info . "</em></ul>" if $info; |
| 429 |
|
| 430 |
print $q->start_form; |
| 431 |
print $q->submit("Block New", "New"), $q->submit("Block Raw New", "Raw New"), $q->submit("Cancel"), "\n"; |
| 432 |
print $q->end_form; |
| 433 |
print "<hr>\n"; |
| 434 |
|
| 435 |
my $ct = new Config::Crontab( -file => $file); $ct->read; |
| 436 |
my $i = 0; |
| 437 |
for my $block ( $ct->blocks ) { |
| 438 |
print $q->start_form; |
| 439 |
$q->param('blockno', $i++); |
| 440 |
print $q->hidden( -name=>'blockno' ); |
| 441 |
print qq!<table border="1">\n!; |
| 442 |
print qq!<tr><td>Delete</td><td>Edit</td><td>Raw<br>Edit</td><td>First</td><td>Up</td><td>Down</td><td>Last</td></tr>\n!; |
| 443 |
print qq!<tr valign="middle">\n!; |
| 444 |
print "<td>", $q->submit("Block Delete", "X"), "</td>\n"; |
| 445 |
print "<td>", $q->submit("Block Edit", "E"), "</td>\n"; |
| 446 |
print "<td>", $q->submit("Block Raw Edit", "R"), "</td>\n"; |
| 447 |
print "<td>", $q->submit("Block First", "|<"), "</td>\n"; |
| 448 |
print "<td>", $q->submit("Block Up", "<"), "</td>\n"; |
| 449 |
print "<td>", $q->submit("Block Down", ">"), "</td>\n"; |
| 450 |
print "<td>", $q->submit("Block Last", ">|"), "</td>\n"; |
| 451 |
print "<td><pre>\n"; |
| 452 |
print $block->dump; |
| 453 |
print "</pre></td>\n"; |
| 454 |
print "</tr></table>\n"; |
| 455 |
|
| 456 |
print $q->end_form; |
| 457 |
print "<hr>\n"; |
| 458 |
print "\n\n"; |
| 459 |
} |
| 460 |
print $q->start_form; |
| 461 |
print $q->submit("Block New", "New"), $q->submit("Block Raw New", "Raw New"), $q->submit("Cancel"), "\n"; |
| 462 |
print $q->end_form; |
| 463 |
print $q->end_html; |
| 464 |
exit; |