test.secret@here 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/perl
  2. ##
  3. ## this is a test script for regressing changes to the secret@here PAM
  4. ## agent
  5. ##
  6. $^W = 1;
  7. use strict;
  8. use IPC::Open2;
  9. $| = 1;
  10. my $whoami = `/usr/bin/whoami`; chomp $whoami;
  11. my $cookie = "12345";
  12. my $user_domain = "$whoami\@local.host";
  13. my $pid = open2(\*Reader, \*Writer, "../agents/secret\@here blah")
  14. or die "failed to load secret\@here agent";
  15. unless (-f (getpwuid($<))[7]."/.secret\@here") {
  16. print STDERR "server: ". "no " .(getpwuid($<))[7]. "/.secret\@here file\n";
  17. die "no config file";
  18. }
  19. WriteBinaryPrompt(\*Writer, 0x02, "secret\@here/$user_domain|$cookie");
  20. my ($control, $data) = ReadBinaryPrompt(\*Reader);
  21. print STDERR "server: ". "reply: control=$control, data=$data\n";
  22. if ($control != 1) {
  23. die "expected 1 (OK) for the first agent reply; got $control";
  24. }
  25. my ($seqid, $a_cookie) = split '\|', $data;
  26. # server needs to convince agent that it knows the secret before
  27. # agent will give a valid response
  28. my $secret = IdentifyLocalSecret($user_domain);
  29. my $digest = CreateDigest($a_cookie."|".$cookie."|".$secret);
  30. print STDERR "server: ". "digest = $digest\n";
  31. WriteBinaryPrompt(\*Writer, 0x01, "$seqid|$digest");
  32. # The agent will authenticate us and then reply with its
  33. # authenticating digest. we check that before we're done.
  34. ($control, $data) = ReadBinaryPrompt(\*Reader);
  35. if ($control != 0x03) {
  36. die "server: agent did not reply with a 'done' prompt ($control)\n";
  37. }
  38. unless ($data eq CreateDigest($secret."|".$cookie."|".$a_cookie)) {
  39. die "server: agent is not authenticated\n";
  40. }
  41. print STDERR "server: agent appears to know secret\n";
  42. my $session_authenticated_ticket
  43. = CreateDigest($cookie."|".$secret."|".$a_cookie);
  44. print STDERR "server: should putenv("
  45. ."\"AUTH_SESSION_TICKET=$session_authenticated_ticket\")\n";
  46. exit 0;
  47. sub CreateDigest ($) {
  48. my ($data) = @_;
  49. my $pid = open2(\*MD5out, \*MD5in, "/usr/bin/md5sum -")
  50. or die "you'll need /usr/bin/md5sum installed";
  51. my $oldfd = select MD5in; $|=1; select $oldfd;
  52. print MD5in "$data";
  53. close MD5in;
  54. my $reply = <MD5out>;
  55. ($reply) = split /\s/, $reply;
  56. print STDERR "server: ". "md5 said: <$reply>\n";
  57. close MD5out;
  58. return $reply;
  59. }
  60. sub ReadBinaryPrompt ($) {
  61. my ($fd) = @_;
  62. my $buffer = " ";
  63. my $count = read($fd, $buffer, 5);
  64. if ($count == 0) {
  65. # no more packets to read
  66. return (0, "");
  67. }
  68. if ($count != 5) {
  69. # broken packet header
  70. return (-1, "");
  71. }
  72. my ($length, $control) = unpack("N C", $buffer);
  73. if ($length < 5) {
  74. # broken packet length
  75. return (-1, "");
  76. }
  77. my $data = "";
  78. $length -= 5;
  79. while ($count = read($fd, $buffer, $length)) {
  80. $data .= $buffer;
  81. if ($count != $length) {
  82. $length -= $count;
  83. next;
  84. }
  85. print STDERR "server: ". "data is [$data]\n";
  86. return ($control, $data);
  87. }
  88. # broken packet data
  89. return (-1, "");
  90. }
  91. sub WriteBinaryPrompt ($$$) {
  92. my ($fd, $control, $data) = @_;
  93. my $length = 5 + length($data);
  94. printf STDERR "server: ". "{%d|0x%.2x|%s}\n", $length, $control, $data;
  95. my $bp = pack("N C a*", $length, $control, $data);
  96. print $fd $bp;
  97. print STDERR "server: ". "control passed to agent\@here\n";
  98. }
  99. sub IdentifyLocalSecret ($) {
  100. my ($identifier) = @_;
  101. my $secret;
  102. my $whoami = `/usr/bin/whoami` ; chomp $whoami;
  103. if (open SECRETS, "< " .(getpwuid($<))[7]. "/.secret\@here") {
  104. my $line;
  105. while (defined ($line = <SECRETS>)) {
  106. my ($id, $sec) = split /[\s]/, $line;
  107. if ((defined $id) && ($id eq $identifier)) {
  108. $secret = $sec;
  109. last;
  110. }
  111. }
  112. close SECRETS;
  113. }
  114. return $secret;
  115. }