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: releng/11.0/contrib/opie/opieinfo.c 257264 2013-10-28 18:24:31Z sbruno $
3478644Sache
3522347Spst*/
3622347Spst
3722347Spst#include "opie_cfg.h"
3881973Smarkm#include <sys/param.h>
3981973Smarkm#include <errno.h>
4022347Spst#include <stdio.h>
41257264Ssbruno#include <stdlib.h>
4281973Smarkm#include <string.h>
4322347Spst#if HAVE_UNISTD_H
4422347Spst#include <unistd.h>
4522347Spst#endif /* HAVE_UNISTD_H */
4622347Spst#include "opie.h"
4722347Spst
4822347Spst/* extern char *optarg; */
4981973Smarkm/* extern int errno, optind; */
5022347Spst
5122347Spststatic char *getusername FUNCTION_NOARGS
5222347Spst{
5378644Sache  char *login;
5422347Spst
5578644Sache  login = getlogin();
5678644Sache  if (login == NULL) {
5778644Sache    fprintf(stderr, "Cannot find login name\n");
5878644Sache    exit(1);
5978644Sache  }
6078644Sache  return login;
6122347Spst}
6222347Spst
6322347Spstint main FUNCTION((argc, argv), int argc AND char *argv[])
6422347Spst{
6522347Spst  char *username;
6622347Spst  struct opie opie;
6722347Spst  int i;
6822347Spst
6922347Spst  while ((i = getopt(argc, argv, "hv")) != EOF) {
7022347Spst    switch (i) {
7122347Spst    case 'v':
7222347Spst      opieversion();
7322347Spst    case 'h':
7422347Spst    default:
7522347Spst      fprintf(stderr, "usage: %s [-h] [-v] [user_name]\n", argv[0]);
7622347Spst      exit(0);
7722347Spst    }
7822347Spst  }
7922347Spst
8078644Sache  if (optind < argc) {
8178644Sache    if (getuid() != 0) {
8278644Sache      fprintf(stderr, "Only superuser may get another user's keys\n");
8378644Sache      exit(1);
8478644Sache    }
8522347Spst    username = argv[optind];
8678644Sache  } else
8722347Spst    username = getusername();
8822347Spst
8981973Smarkm  if (strlen(username) >= MAXLOGNAME) {
9081973Smarkm    fprintf(stderr, "Username too long.\n");
9181973Smarkm    exit(1);
9281973Smarkm  }
9381973Smarkm
9422347Spst  if ((i = opielookup(&opie, username)) && (i != 2)) {
9522347Spst    if (i < 0)
9622347Spst      fprintf(stderr, "Error opening database! (errno = %d)\n", errno);
9722347Spst    else
9822347Spst      fprintf(stderr, "%s not found in database.\n", username);
9922347Spst    exit(1);
10022347Spst  }
10122347Spst
10222347Spst  printf("%d %s\n", opie.opie_n - 1, opie.opie_seed);
10322347Spst
10422347Spst  return 0;
10522347Spst}
106