#include <stdio.h>
#include <unistd.h>
#include <error.h>

#include <mach.h>
#include <hurd.h>
#include <hurd/paths.h>
#include <hurd/password.h>
#include <hurd/msg.h>

int
main (int argc, char *argv [])
{
  error_t err;
  mach_port_t password_server, msgport;
  auth_t auth;
  process_t proc_server = getproc ();

  if (argc != 3)
    error (1, 0, "Usage: %s user password", argv[0]);

  password_server = file_name_lookup (_SERVERS_PASSWORD, 0, 0);
  if (password_server == MACH_PORT_NULL)
    error (1, 0, "Failed to obtain a port to the password server");

  err = password_check_user (password_server, atoi (argv[1]), argv[2], &auth);
  if (err == EACCES)
    error (1, err, "Invalid password");
  if (err)
    error (1, err, "Authentication failure");

  err = proc_getmsgport (proc_server, getppid (), &msgport);
  if (err)
    error (1, err, "Cannot get parent's message port");

  err = msg_add_auth (msgport, auth);
  if (err)
    error (1, err, "Failed to add authorization to parent");

  return 0;
}
  
  

  

  
