/example/daemon.pl

https://github.com/gitpan/dsmlrpc · Perl · 96 lines · 71 code · 19 blank · 6 comment · 14 complexity · 455b2b024052902d79764d6cb2e2bbc1 MD5 · raw file

  1. #!/usr/bin/perl
  2. use strict;
  3. use HTTP::Daemon;
  4. use HTTP::Status;
  5. #use Data::Dumper;
  6. use Shell;
  7. use POSIX ":sys_wait_h";
  8. use DSMLLDAP;
  9. sub zombie_reaper {
  10. while (waitpid(-1, WNOHANG) > 0)
  11. { }
  12. $SIG{CHLD} = \&zombie_reaper;
  13. }
  14. $SIG{CHLD} = \&zombie_reaper;
  15. my $child_pid = fork;
  16. if ( $child_pid ) {
  17. exit;
  18. }
  19. my $user = shift @ARGV || "cn=admin,dc=yacme,dc=com";
  20. my $pwd = shift @ARGV || "pwd";
  21. my $host = shift @ARGV || '127.0.0.1';
  22. my $port = shift @ARGV || 6890;
  23. my $d = HTTP::Daemon->new(LocalAddr => $host,
  24. LocalPort => $port,
  25. Reuse => 1) || die;
  26. POSIX::setuid(65534);
  27. POSIX::setgid(65533);
  28. print "Please contact me at: <URL:", $d->url, ">\n";
  29. while (1) {
  30. while (my $client = $d->accept) {
  31. my $child_pid = fork;
  32. if ( $child_pid ) {
  33. next;
  34. }
  35. elsif (defined ($child_pid)) {
  36. HttpdChild($client);
  37. exit;
  38. }
  39. else {
  40. print "HTTPD: fork failed: $!\n";
  41. }
  42. $client = undef;
  43. }
  44. continue {
  45. $client->close;
  46. undef ($client);
  47. }
  48. }
  49. sub HttpdChild {
  50. my $httpd = shift;
  51. while (my $r = $httpd->get_request) {
  52. my $xml;
  53. my $xmlfile;
  54. my $local = time; chop $local;
  55. $xmlfile = "XML".$local;
  56. if ($r->method eq 'GET') {
  57. $httpd->send_error(RC_FORBIDDEN);
  58. }
  59. elsif ($r->method eq 'POST') {
  60. my $req = $r->content;
  61. # open OUT, "> /tmp/req.xml";
  62. # print OUT $req;
  63. # close OUT;
  64. my $ldapObj = DSMLLDAP->new($user,$pwd);
  65. #$xml = $ldapObj->handle($req, $user, $pwd);
  66. $xml = $ldapObj->handle($req);
  67. open OUT, "> /tmp/$xmlfile";
  68. print OUT $xml;
  69. close OUT;
  70. $httpd->send_file_response("/tmp/$xmlfile");
  71. rm("/tmp/$xmlfile");
  72. }
  73. else {
  74. $httpd->send_error(RC_FORBIDDEN);
  75. }
  76. }
  77. }