opieinfo.c revision 29964
1240116Smarcel/*
2240116Smarcelopieinfo: Print a user's current OPIE sequence number and seed
3240116Smarcel
4240116Smarcel%%% portions-copyright-cmetz-96
5240116SmarcelPortions of this software are Copyright 1996-1997 by Craig Metz, All Rights
6240116SmarcelReserved. The Inner Net License Version 2 applies to these portions of
7240116Smarcelthe software.
8240116SmarcelYou should have received a copy of the license with this software. If
9240116Smarcelyou didn't get a copy, you may request one from <license@inner.net>.
10240116Smarcel
11240116SmarcelPortions of this software are Copyright 1995 by Randall Atkinson and Dan
12240116SmarcelMcDonald, All Rights Reserved. All Rights under this copyright are assigned
13240116Smarcelto the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
14240116SmarcelLicense Agreement applies to this software.
15240116Smarcel
16240116Smarcel	History:
17240116Smarcel
18240116Smarcel	Modified by cmetz for OPIE 2.3. Removed unneeded debug message.
19240116Smarcel	Modified by cmetz for OPIE 2.2. Use FUNCTION definition et al.
20240116Smarcel               Fixed include order. Make everything static. Ifdef around
21240116Smarcel               some headers.
22240116Smarcel        Modified at NRL for OPIE 2.1. Substitute @@KEY_FILE@@. Re-write in
23240116Smarcel	       C.
24240116Smarcel        Modified at NRL for OPIE 2.01. Remove hard-coded paths for grep and
25240116Smarcel               awk and let PATH take care of it. Substitute for Makefile
26240116Smarcel               variables $(EXISTS) and $(KEY_FILE). Only compute $WHO if
27240116Smarcel               there's a key file. Got rid of grep since awk can do the job
28240116Smarcel               itself.
29240116Smarcel	Modified at NRL for OPIE 2.0.
30240116Smarcel	Written at Bellcore for the S/Key Version 1 software distribution
31240116Smarcel		(keyinfo)
32240116Smarcel*/
33240116Smarcel
34240116Smarcel#include "opie_cfg.h"
35240116Smarcel#include <stdio.h>
36240116Smarcel#if HAVE_UNISTD_H
37240116Smarcel#include <unistd.h>
38240116Smarcel#endif /* HAVE_UNISTD_H */
39240116Smarcel#if HAVE_PWD_H
40240116Smarcel#include <pwd.h>
41240116Smarcel#endif /* HAVE_PWD_H */
42240116Smarcel#include "opie.h"
43240116Smarcel
44240116Smarcel/* extern char *optarg; */
45240116Smarcelextern int errno, optind;
46240116Smarcel
47240116Smarcelstatic char *getusername FUNCTION_NOARGS
48240116Smarcel{
49240116Smarcel  struct passwd *p = getpwuid(getuid());
50240116Smarcel
51240116Smarcel  if (!p)
52240116Smarcel    return getlogin();
53240116Smarcel
54240116Smarcel  return p->pw_name;
55240116Smarcel}
56240116Smarcel
57240116Smarcelint main FUNCTION((argc, argv), int argc AND char *argv[])
58240116Smarcel{
59240116Smarcel  char *username;
60240116Smarcel  struct opie opie;
61240116Smarcel  int i;
62240116Smarcel
63240116Smarcel  while ((i = getopt(argc, argv, "hv")) != EOF) {
64240116Smarcel    switch (i) {
65240116Smarcel    case 'v':
66240116Smarcel      opieversion();
67240116Smarcel    case 'h':
68240116Smarcel    default:
69240116Smarcel      fprintf(stderr, "usage: %s [-h] [-v] [user_name]\n", argv[0]);
70240116Smarcel      exit(0);
71240116Smarcel    }
72240116Smarcel  }
73240116Smarcel
74240116Smarcel  if (optind < argc)
75240116Smarcel    username = argv[optind];
76240116Smarcel  else
77240116Smarcel    username = getusername();
78240116Smarcel
79240116Smarcel  if ((i = opielookup(&opie, username)) && (i != 2)) {
80240116Smarcel    if (i < 0)
81240116Smarcel      fprintf(stderr, "Error opening database! (errno = %d)\n", errno);
82240116Smarcel    else
83240116Smarcel      fprintf(stderr, "%s not found in database.\n", username);
84240116Smarcel    exit(1);
85240116Smarcel  }
86240116Smarcel
87240116Smarcel  printf("%d %s\n", opie.opie_n - 1, opie.opie_seed);
88240116Smarcel
89240116Smarcel  return 0;
90240116Smarcel}
91240116Smarcel