xref: /openbsd/usr.bin/skeyinfo/skeyinfo.c (revision d485f761)
1 /*	$OpenBSD: skeyinfo.c,v 1.9 2001/06/19 01:49:45 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <err.h>
31 #include <pwd.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <skey.h>
36 #include <login_cap.h>
37 #include <bsd_auth.h>
38 
39 extern char *__progname;
40 
41 void usage(void);
42 
43 int
44 main(int argc, char **argv)
45 {
46 	struct passwd *pw;
47 	char *style, *challenge, *cp, *name;
48 	int ch, verbose = 0;
49 	login_cap_t *lc;
50 	auth_session_t *as;
51 
52 	name = NULL;
53 	style = "skey";
54 	while ((ch = getopt(argc, argv, "a:v")) != -1)
55 		switch(ch) {
56 		case 'a':
57 			style = optarg;
58 			break;
59 		case 'v':
60 			verbose = 1;
61 			break;
62 		default:
63 			usage();
64 	}
65 	argc -= optind;
66 	argv += optind;
67 
68 	if (argc == 1)
69 		name = argv[0];
70 	else if (argc > 1)
71 		usage();
72 
73 	if (name && getuid() != 0)
74 		errx(1, "only root may specify an alternate user");
75 
76 	if (name) {
77 		if ((pw = getpwnam(name)) == NULL)
78 			errx(1, "no passwd entry for %s", name);
79 	} else {
80 		if ((pw = getpwuid(getuid())) == NULL)
81 			errx(1, "no passwd entry for uid %u", getuid());
82 	}
83 
84 	if ((name = strdup(pw->pw_name)) == NULL)
85 		err(1, "cannot allocate memory");
86 
87 	if ((lc = login_getclass(pw->pw_class)) == NULL)
88 		errx(1, "unable to classify user %s", name);
89 
90 	if ((cp = login_getstyle(lc, style, NULL)) == NULL)
91 		errx(1, "unknown authentication method %s", style);
92 
93 	as = auth_userchallenge(name, cp, NULL, &challenge);
94 	if (as == NULL || challenge == NULL) {
95 		if (as)
96 			auth_close(as);
97 		errx(1, "unable to retrieve challenge for %s", name);
98 	}
99 
100 	/*
101 	 * We only want the first line of the challenge so stop after a newline.
102 	 * If the user wants the full challenge including the hash type
103 	 * or if the challenge didn't start with 'otp-', print it verbatim.
104 	 * Otherwise, strip off the first word.
105 	 */
106 	if ((cp = strchr(challenge, '\n')))
107 		*cp = '\0';
108 	cp = strchr(challenge, ' ');
109 	if (verbose || *challenge != 'o' || !cp)
110 		cp = challenge;
111 	else
112 		cp++;
113 	puts(cp);
114 
115 	auth_close(as);
116 	exit(0);
117 }
118 
119 void
120 usage(void)
121 {
122 
123 	(void)fprintf(stderr, "Usage: %s [-a auth-type] [-v] [user]\n",
124 	    __progname);
125 	exit(1);
126 }
127