122347Spst/*
222347Spstopieinfo: Print a user's current OPIE sequence number and seed
322347Spst
429964Sache%%% portions-copyright-cmetz-96
592914SmarkmPortions of this software are Copyright 1996-1999 by Craig Metz, All Rights
622347SpstReserved. The Inner Net License Version 2 applies to these portions of
722347Spstthe software.
822347SpstYou should have received a copy of the license with this software. If
922347Spstyou didn't get a copy, you may request one from <license@inner.net>.
1022347Spst
1122347SpstPortions of this software are Copyright 1995 by Randall Atkinson and Dan
1222347SpstMcDonald, All Rights Reserved. All Rights under this copyright are assigned
1322347Spstto the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
1422347SpstLicense Agreement applies to this software.
1522347Spst
1622347Spst	History:
1722347Spst
1822347Spst	Modified by cmetz for OPIE 2.3. Removed unneeded debug message.
1922347Spst	Modified by cmetz for OPIE 2.2. Use FUNCTION definition et al.
2022347Spst               Fixed include order. Make everything static. Ifdef around
2122347Spst               some headers.
2222347Spst        Modified at NRL for OPIE 2.1. Substitute @@KEY_FILE@@. Re-write in
2322347Spst	       C.
2422347Spst        Modified at NRL for OPIE 2.01. Remove hard-coded paths for grep and
2522347Spst               awk and let PATH take care of it. Substitute for Makefile
2622347Spst               variables $(EXISTS) and $(KEY_FILE). Only compute $WHO if
2722347Spst               there's a key file. Got rid of grep since awk can do the job
2822347Spst               itself.
2922347Spst	Modified at NRL for OPIE 2.0.
3022347Spst	Written at Bellcore for the S/Key Version 1 software distribution
3122347Spst		(keyinfo)
3278644Sache
3378644Sache$FreeBSD$
3478644Sache
3522347Spst*/
3622347Spst
3722347Spst#include "opie_cfg.h"
3881973Smarkm#include <sys/param.h>
3981973Smarkm#include <errno.h>
4022347Spst#include <stdio.h>
4181973Smarkm#include <string.h>
4222347Spst#if HAVE_UNISTD_H
4322347Spst#include <unistd.h>
4422347Spst#endif /* HAVE_UNISTD_H */
4522347Spst#include "opie.h"
4622347Spst
4722347Spst/* extern char *optarg; */
4881973Smarkm/* extern int errno, optind; */
4922347Spst
5022347Spststatic char *getusername FUNCTION_NOARGS
5122347Spst{
5278644Sache  char *login;
5322347Spst
5478644Sache  login = getlogin();
5578644Sache  if (login == NULL) {
5678644Sache    fprintf(stderr, "Cannot find login name\n");
5778644Sache    exit(1);
5878644Sache  }
5978644Sache  return login;
6022347Spst}
6122347Spst
6222347Spstint main FUNCTION((argc, argv), int argc AND char *argv[])
6322347Spst{
6422347Spst  char *username;
6522347Spst  struct opie opie;
6622347Spst  int i;
6722347Spst
6822347Spst  while ((i = getopt(argc, argv, "hv")) != EOF) {
6922347Spst    switch (i) {
7022347Spst    case 'v':
7122347Spst      opieversion();
7222347Spst    case 'h':
7322347Spst    default:
7422347Spst      fprintf(stderr, "usage: %s [-h] [-v] [user_name]\n", argv[0]);
7522347Spst      exit(0);
7622347Spst    }
7722347Spst  }
7822347Spst
7978644Sache  if (optind < argc) {
8078644Sache    if (getuid() != 0) {
8178644Sache      fprintf(stderr, "Only superuser may get another user's keys\n");
8278644Sache      exit(1);
8378644Sache    }
8422347Spst    username = argv[optind];
8578644Sache  } else
8622347Spst    username = getusername();
8722347Spst
8881973Smarkm  if (strlen(username) >= MAXLOGNAME) {
8981973Smarkm    fprintf(stderr, "Username too long.\n");
9081973Smarkm    exit(1);
9181973Smarkm  }
9281973Smarkm
9322347Spst  if ((i = opielookup(&opie, username)) && (i != 2)) {
9422347Spst    if (i < 0)
9522347Spst      fprintf(stderr, "Error opening database! (errno = %d)\n", errno);
9622347Spst    else
9722347Spst      fprintf(stderr, "%s not found in database.\n", username);
9822347Spst    exit(1);
9922347Spst  }
10022347Spst
10122347Spst  printf("%d %s\n", opie.opie_n - 1, opie.opie_seed);
10222347Spst
10322347Spst  return 0;
10422347Spst}
105