/*
opieinfo: Print a user's current OPIE sequence number and seed

Portions of this software are Copyright 1995 by Randall Atkinson and Dan
McDonald, All Rights Reserved. All Rights under this copyright are assigned
to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
License Agreement applies to this software.

	History:

        Modified at NRL for OPIE 2.1. Substitute @@KEY_FILE@@. Re-write in
	       C.
        Modified at NRL for OPIE 2.01. Remove hard-coded paths for grep and
               awk and let PATH take care of it. Substitute for Makefile 
               variables $(EXISTS) and $(KEY_FILE). Only compute $WHO if 
               there's a key file. Got rid of grep since awk can do the job
               itself.
	Modified at NRL for OPIE 2.0.
	Written at Bellcore for the S/Key Version 1 software distribution
		(keyinfo)
*/

#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include "opie.h"
#include "opie_cfg.h"

/* extern char *optarg; */
extern int errno, optind;

char *getusername()
{
  struct passwd *p = getpwuid(getuid());

  if (!p)
    return getlogin();

  return p->pw_name;
}

main(argc, argv)
int argc;
char *argv[];
{
  FILE *f;
  char *username;
  struct opie opie;
  int i;

  while ((i = getopt(argc, argv, "hv")) != EOF) {
    switch (i) {
    case 'v':
      opieversion();
    case 'h':
    default:
      fprintf(stderr, "usage: %s [-h] [-v] [user_name]\n", argv[0]);
      exit(0);
    }
  }

  if (optind < argc)
    username = argv[optind];
  else
    username = getusername();

#if DEBUG
  fprintf(stderr, "Looking up %s... ", username);
#endif /* DEBUG */

  if ((i = opielookup(&opie, username)) && (i != 2)) {
    if (i < 0)
      fprintf(stderr, "Error opening database! (errno = %d)\n", errno);
    else
      fprintf(stderr, "%s not found in database.\n", username);
    exit(1);
  }

  printf("%d %s\n", opie.n - 1, opie.seed);

  return 0;
}
