xref: /titanic_52/usr/src/cmd/keyserv/keylogin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Set secret key on local machine
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate #include <stdio.h>
46*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
47*7c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
48*7c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>				/* to revoke existing creds */
49*7c478bd9Sstevel@tonic-gate #include <nfs/nfssys.h>
50*7c478bd9Sstevel@tonic-gate #include <string.h>
51*7c478bd9Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE "/etc/.rootkey"
54*7c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE_BACKUP	"/etc/.rootkey.bak"
55*7c478bd9Sstevel@tonic-gate /* Should last until 16384-bit DH keys */
56*7c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LINE_LEN	4224
57*7c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LEN		4096
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate extern int key_setnet_g();
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static void logout_curr_key();
62*7c478bd9Sstevel@tonic-gate static int mkrootkey;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static char *sec_domain = NULL;
65*7c478bd9Sstevel@tonic-gate static char local_domain[MAXNETNAMELEN + 1];
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * fgets is broken in that if it reads a NUL character it will always return
69*7c478bd9Sstevel@tonic-gate  * EOF.  This replacement can deal with NULs
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate static char *
72*7c478bd9Sstevel@tonic-gate fgets_ignorenul(char *s, int n, FILE *stream)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	int fildes = fileno(stream);
75*7c478bd9Sstevel@tonic-gate 	int i = 0;
76*7c478bd9Sstevel@tonic-gate 	int rs = 0;
77*7c478bd9Sstevel@tonic-gate 	char c;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (fildes < 0)
80*7c478bd9Sstevel@tonic-gate 		return (NULL);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	while (i < n - 1) {
83*7c478bd9Sstevel@tonic-gate 		rs = read(fildes, &c, 1);
84*7c478bd9Sstevel@tonic-gate 		switch (rs) {
85*7c478bd9Sstevel@tonic-gate 		case 1:
86*7c478bd9Sstevel@tonic-gate 			break;
87*7c478bd9Sstevel@tonic-gate 		case 0:
88*7c478bd9Sstevel@tonic-gate 			/* EOF */
89*7c478bd9Sstevel@tonic-gate 			if (i > 0)
90*7c478bd9Sstevel@tonic-gate 				s[i] = '\0';
91*7c478bd9Sstevel@tonic-gate 			return (NULL);
92*7c478bd9Sstevel@tonic-gate 			break;
93*7c478bd9Sstevel@tonic-gate 		default:
94*7c478bd9Sstevel@tonic-gate 			return (NULL);
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 		switch (c) {
97*7c478bd9Sstevel@tonic-gate 		case '\0':
98*7c478bd9Sstevel@tonic-gate 			break;
99*7c478bd9Sstevel@tonic-gate 		case '\n':
100*7c478bd9Sstevel@tonic-gate 			s[i] = c;
101*7c478bd9Sstevel@tonic-gate 			s[++i] = '\0';
102*7c478bd9Sstevel@tonic-gate 			return (s);
103*7c478bd9Sstevel@tonic-gate 		default:
104*7c478bd9Sstevel@tonic-gate 		if (c != '\0')
105*7c478bd9Sstevel@tonic-gate 			s[i++] = c;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	s[i] = '\0';
109*7c478bd9Sstevel@tonic-gate 	return (s);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate /* write unencrypted secret key into root key file */
114*7c478bd9Sstevel@tonic-gate static void
115*7c478bd9Sstevel@tonic-gate write_rootkey(char *secret, char *flavor, keylen_t keylen, algtype_t algtype)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	char		line[MAXROOTKEY_LINE_LEN];
118*7c478bd9Sstevel@tonic-gate 	char		keyent[MAXROOTKEY_LEN];
119*7c478bd9Sstevel@tonic-gate 	algtype_t	atent;
120*7c478bd9Sstevel@tonic-gate 	int		rootfd, bakfd, hexkeybytes;
121*7c478bd9Sstevel@tonic-gate 	bool_t		lineone = TRUE;
122*7c478bd9Sstevel@tonic-gate 	bool_t		gotit = FALSE;
123*7c478bd9Sstevel@tonic-gate 	FILE		*rootfile, *bakfile;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
126*7c478bd9Sstevel@tonic-gate 	if ((rename(ROOTKEY_FILE, ROOTKEY_FILE_BACKUP)) < 0) {
127*7c478bd9Sstevel@tonic-gate 		if ((bakfd = creat(ROOTKEY_FILE_BACKUP, 0600)) < 0) {
128*7c478bd9Sstevel@tonic-gate 			perror("Could not create /etc/.rootkey.bak");
129*7c478bd9Sstevel@tonic-gate 			goto rootkey_err;
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 		close(bakfd);
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if ((rootfd = open(ROOTKEY_FILE, O_WRONLY+O_CREAT, 0600)) < 0) {
135*7c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
136*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
137*7c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
138*7c478bd9Sstevel@tonic-gate 		(void) rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
139*7c478bd9Sstevel@tonic-gate 		goto rootkey_err;
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	if (!(rootfile = fdopen(rootfd, "w"))) {
142*7c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
143*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
144*7c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
145*7c478bd9Sstevel@tonic-gate 		close(rootfd);
146*7c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
147*7c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
148*7c478bd9Sstevel@tonic-gate 		goto rootkey_err;
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	if (!(bakfile = fopen(ROOTKEY_FILE_BACKUP, "r"))) {
151*7c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey.bak for reading");
152*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
153*7c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
154*7c478bd9Sstevel@tonic-gate 		(void) fclose(rootfile);
155*7c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
156*7c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
157*7c478bd9Sstevel@tonic-gate 		goto rootkey_err;
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	hexkeybytes = ((keylen + 7) / 8) * 2;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	while (fgets_ignorenul(line, MAXROOTKEY_LINE_LEN, bakfile)) {
163*7c478bd9Sstevel@tonic-gate 		(void) sscanf(line, "%s %d", keyent, &atent);
164*7c478bd9Sstevel@tonic-gate 		/*
165*7c478bd9Sstevel@tonic-gate 		 * 192-bit keys always go on the first line
166*7c478bd9Sstevel@tonic-gate 		 */
167*7c478bd9Sstevel@tonic-gate 		if (lineone) {
168*7c478bd9Sstevel@tonic-gate 			lineone = FALSE;
169*7c478bd9Sstevel@tonic-gate 			if (keylen == 192) {
170*7c478bd9Sstevel@tonic-gate 				gotit = TRUE;
171*7c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s\n", secret);
172*7c478bd9Sstevel@tonic-gate 			} else
173*7c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
174*7c478bd9Sstevel@tonic-gate 			(void) fflush(rootfile);
175*7c478bd9Sstevel@tonic-gate 		} else {
176*7c478bd9Sstevel@tonic-gate 			if ((strlen(keyent) == hexkeybytes) &&
177*7c478bd9Sstevel@tonic-gate 			    (atent == algtype)) {
178*7c478bd9Sstevel@tonic-gate 				/*
179*7c478bd9Sstevel@tonic-gate 				 * Silently remove lines with the same
180*7c478bd9Sstevel@tonic-gate 				 * keylen/algtype
181*7c478bd9Sstevel@tonic-gate 				 */
182*7c478bd9Sstevel@tonic-gate 				if (gotit)
183*7c478bd9Sstevel@tonic-gate 					continue;
184*7c478bd9Sstevel@tonic-gate 				else
185*7c478bd9Sstevel@tonic-gate 					gotit = TRUE;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s %d\n", secret, algtype);
188*7c478bd9Sstevel@tonic-gate 			} else
189*7c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
190*7c478bd9Sstevel@tonic-gate 			(void) fflush(rootfile);
191*7c478bd9Sstevel@tonic-gate 		}
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	/* Append key to rootkey file */
195*7c478bd9Sstevel@tonic-gate 	if (!gotit) {
196*7c478bd9Sstevel@tonic-gate 		if (keylen == 192)
197*7c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s\n", secret);
198*7c478bd9Sstevel@tonic-gate 		else {
199*7c478bd9Sstevel@tonic-gate 			if (lineone)
200*7c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "\n");
201*7c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s %d\n", secret, algtype);
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 	(void) fflush(rootfile);
205*7c478bd9Sstevel@tonic-gate 	fclose(rootfile);
206*7c478bd9Sstevel@tonic-gate 	fclose(bakfile);
207*7c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
208*7c478bd9Sstevel@tonic-gate 	if (keylen == 192)
209*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Wrote secret key into %s\n", ROOTKEY_FILE);
210*7c478bd9Sstevel@tonic-gate 	else
211*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Wrote %s key into %s\n", flavor,
212*7c478bd9Sstevel@tonic-gate 			ROOTKEY_FILE);
213*7c478bd9Sstevel@tonic-gate 	return;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate rootkey_err:
216*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "WARNING: Could not write %s key to /etc/.rootkey\n",
217*7c478bd9Sstevel@tonic-gate 		flavor);
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate /* Perform AUTH_DES keylogin */
221*7c478bd9Sstevel@tonic-gate static int
222*7c478bd9Sstevel@tonic-gate oldkeylogin(char *fullname, char *pass)
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	char			secret[HEXKEYBYTES+1];
225*7c478bd9Sstevel@tonic-gate 	struct key_netstarg	netst;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		if (getsecretkey(fullname, secret, pass) == 0) {
228*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not find %s's secret key\n",
229*7c478bd9Sstevel@tonic-gate 				fullname);
230*7c478bd9Sstevel@tonic-gate 			if (sec_domain && *sec_domain &&
231*7c478bd9Sstevel@tonic-gate 				strcasecmp(sec_domain, local_domain)) {
232*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
233*7c478bd9Sstevel@tonic-gate "The system default domain '%s' is different from the Secure RPC\n\
234*7c478bd9Sstevel@tonic-gate domain %s where the key is stored.  The Secure RPC domainname is\n\
235*7c478bd9Sstevel@tonic-gate defined by the directory object stored in the /var/nis/NIS_COLD_START file.\n\
236*7c478bd9Sstevel@tonic-gate If you need to change this Secure RPC domainname, please use the nisinit(1M)\n\
237*7c478bd9Sstevel@tonic-gate command with the `-k` option.\n", local_domain, sec_domain);
238*7c478bd9Sstevel@tonic-gate 			} else {
239*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
240*7c478bd9Sstevel@tonic-gate 		"Make sure the secret key is stored in domain %s\n",
241*7c478bd9Sstevel@tonic-gate 				local_domain);
242*7c478bd9Sstevel@tonic-gate 			}
243*7c478bd9Sstevel@tonic-gate 			return (1);
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 		if (secret[0] == 0) {
247*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Password incorrect for %s\n",
248*7c478bd9Sstevel@tonic-gate 				fullname);
249*7c478bd9Sstevel@tonic-gate 			return (1);
250*7c478bd9Sstevel@tonic-gate 		}
251*7c478bd9Sstevel@tonic-gate 		/* revoke any existing (lingering) credentials... */
252*7c478bd9Sstevel@tonic-gate 		logout_curr_key();
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 		memcpy(netst.st_priv_key, secret, HEXKEYBYTES);
255*7c478bd9Sstevel@tonic-gate 		memset(secret, 0, HEXKEYBYTES);
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 		netst.st_pub_key[0] = 0;
258*7c478bd9Sstevel@tonic-gate 		netst.st_netname = strdup(fullname);
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 		/* do actual key login */
261*7c478bd9Sstevel@tonic-gate 		if (key_setnet(&netst) < 0) {
262*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not set %s's secret key\n",
263*7c478bd9Sstevel@tonic-gate 				fullname);
264*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "May be the keyserv is down?\n");
265*7c478bd9Sstevel@tonic-gate 			if (mkrootkey == 0)   /* nothing else to do */
266*7c478bd9Sstevel@tonic-gate 				return (1);
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		/* write unencrypted secret key into root key file */
270*7c478bd9Sstevel@tonic-gate 		if (mkrootkey)
271*7c478bd9Sstevel@tonic-gate 			write_rootkey(netst.st_priv_key, "des", 192, 0);
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 		return (0);
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate  * Revokes the existing credentials for Secure-RPC and Secure-NFS.
278*7c478bd9Sstevel@tonic-gate  * This should only be called if the user entered the correct password;
279*7c478bd9Sstevel@tonic-gate  * sorta like the way "su" doesn't force a login if you enter the wrong
280*7c478bd9Sstevel@tonic-gate  * password.
281*7c478bd9Sstevel@tonic-gate  */
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate static void
284*7c478bd9Sstevel@tonic-gate logout_curr_key()
285*7c478bd9Sstevel@tonic-gate {
286*7c478bd9Sstevel@tonic-gate 	static char		secret[HEXKEYBYTES + 1];
287*7c478bd9Sstevel@tonic-gate 	struct nfs_revauth_args	nra;
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	/*
290*7c478bd9Sstevel@tonic-gate 	 * try to revoke the existing key/credentials, assuming
291*7c478bd9Sstevel@tonic-gate 	 * one exists.  this will effectively mark "stale" any
292*7c478bd9Sstevel@tonic-gate 	 * cached credientials...
293*7c478bd9Sstevel@tonic-gate 	 */
294*7c478bd9Sstevel@tonic-gate 	if (key_setsecret(secret) < 0) {
295*7c478bd9Sstevel@tonic-gate 		return;
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/*
299*7c478bd9Sstevel@tonic-gate 	 * it looks like a credential already existed, so try and
300*7c478bd9Sstevel@tonic-gate 	 * revoke any lingering Secure-NFS privledges.
301*7c478bd9Sstevel@tonic-gate 	 */
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	nra.authtype = AUTH_DES;
304*7c478bd9Sstevel@tonic-gate 	nra.uid = getuid();
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	(void) _nfssys(NFS_REVAUTH, &nra);
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate void
310*7c478bd9Sstevel@tonic-gate usage(cmd)
311*7c478bd9Sstevel@tonic-gate 	char *cmd;
312*7c478bd9Sstevel@tonic-gate {
313*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "usage: %s [-r]\n", cmd);
314*7c478bd9Sstevel@tonic-gate 	exit(1);
315*7c478bd9Sstevel@tonic-gate }
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate main(argc, argv)
319*7c478bd9Sstevel@tonic-gate 	int argc;
320*7c478bd9Sstevel@tonic-gate 	char *argv[];
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate 	char		secret[4096];
323*7c478bd9Sstevel@tonic-gate 	char		fullname[MAXNETNAMELEN + 1];
324*7c478bd9Sstevel@tonic-gate 	char		*getpass();
325*7c478bd9Sstevel@tonic-gate 	char		*pass;
326*7c478bd9Sstevel@tonic-gate 	int		i = 0;
327*7c478bd9Sstevel@tonic-gate 	mechanism_t	**mechlist;
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	if (argc == 1)
330*7c478bd9Sstevel@tonic-gate 		mkrootkey = 0;
331*7c478bd9Sstevel@tonic-gate 	else if (argc == 2 && (strcmp(argv[1], "-r") == 0)) {
332*7c478bd9Sstevel@tonic-gate 		if (geteuid() != 0) {
333*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Must be root to use -r option.\n");
334*7c478bd9Sstevel@tonic-gate 			exit(1);
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 		mkrootkey = 1;
337*7c478bd9Sstevel@tonic-gate 	} else
338*7c478bd9Sstevel@tonic-gate 		usage(argv[0]);
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	if (getnetname(fullname) == 0) {
341*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Could not generate netname\n");
342*7c478bd9Sstevel@tonic-gate 		exit(1);
343*7c478bd9Sstevel@tonic-gate 	}
344*7c478bd9Sstevel@tonic-gate 	sec_domain = strdup(strchr(fullname, '@') + 1);
345*7c478bd9Sstevel@tonic-gate 	getdomainname(local_domain, MAXNETNAMELEN);
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	if (!(pass = getpass("Password:")))
348*7c478bd9Sstevel@tonic-gate 		exit(1);
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	if (mechlist = __nis_get_mechanisms(FALSE)) {
351*7c478bd9Sstevel@tonic-gate 		while (mechlist[i]) {
352*7c478bd9Sstevel@tonic-gate 			char		*alias;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 			if (AUTH_DES_COMPAT_CHK(mechlist[i])) {
355*7c478bd9Sstevel@tonic-gate 				(void) oldkeylogin(fullname, pass);
356*7c478bd9Sstevel@tonic-gate 				i++;
357*7c478bd9Sstevel@tonic-gate 				continue;
358*7c478bd9Sstevel@tonic-gate 			}
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 			if (VALID_ALIAS(mechlist[i]->alias))
361*7c478bd9Sstevel@tonic-gate 				alias = mechlist[i]->alias;
362*7c478bd9Sstevel@tonic-gate 			else
363*7c478bd9Sstevel@tonic-gate 				alias = "";
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 			if (getsecretkey_g(fullname, mechlist[i]->keylen,
366*7c478bd9Sstevel@tonic-gate 						mechlist[i]->algtype, secret,
367*7c478bd9Sstevel@tonic-gate 						(((mechlist[i]->keylen / 7) +
368*7c478bd9Sstevel@tonic-gate 						8) * 2) + 1, pass) == 0) {
369*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
370*7c478bd9Sstevel@tonic-gate 				"WARNING: Could not find %s's %s secret key\n",
371*7c478bd9Sstevel@tonic-gate 					fullname, alias);
372*7c478bd9Sstevel@tonic-gate 				i++;
373*7c478bd9Sstevel@tonic-gate 				continue;
374*7c478bd9Sstevel@tonic-gate 			}
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 			if (secret[0] == 0) {
377*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
378*7c478bd9Sstevel@tonic-gate 				    "Password incorrect for %s's %s key.\n",
379*7c478bd9Sstevel@tonic-gate 					fullname, alias);
380*7c478bd9Sstevel@tonic-gate 				i++;
381*7c478bd9Sstevel@tonic-gate 				continue;
382*7c478bd9Sstevel@tonic-gate 			}
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 			if (key_setnet_g(fullname, secret,
385*7c478bd9Sstevel@tonic-gate 						mechlist[i]->keylen, NULL, 0,
386*7c478bd9Sstevel@tonic-gate 						mechlist[i]->algtype) < 0) {
387*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
388*7c478bd9Sstevel@tonic-gate 				"Could not set %s's %s secret key\n",
389*7c478bd9Sstevel@tonic-gate 					fullname, alias);
390*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
391*7c478bd9Sstevel@tonic-gate 					"May be the keyserv is down?\n");
392*7c478bd9Sstevel@tonic-gate 				exit(1);
393*7c478bd9Sstevel@tonic-gate 			}
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 			if (mkrootkey)
396*7c478bd9Sstevel@tonic-gate 				write_rootkey(secret, mechlist[i]->alias,
397*7c478bd9Sstevel@tonic-gate 						mechlist[i]->keylen,
398*7c478bd9Sstevel@tonic-gate 						mechlist[i]->algtype);
399*7c478bd9Sstevel@tonic-gate 			i++;
400*7c478bd9Sstevel@tonic-gate 		}
401*7c478bd9Sstevel@tonic-gate 	} else
402*7c478bd9Sstevel@tonic-gate 		exit(oldkeylogin(fullname, pass));
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	exit(0);
405*7c478bd9Sstevel@tonic-gate }
406