xref: /illumos-gate/usr/src/cmd/keyserv/keylogin.c (revision a0368f78)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a0368f78Speteh  * Common Development and Distribution License (the "License").
6*a0368f78Speteh  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a0368f78Speteh  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Set secret key on local machine
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
467c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
477c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>				/* to revoke existing creds */
487c478bd9Sstevel@tonic-gate #include <nfs/nfssys.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE "/etc/.rootkey"
537c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE_BACKUP	"/etc/.rootkey.bak"
547c478bd9Sstevel@tonic-gate /* Should last until 16384-bit DH keys */
557c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LINE_LEN	4224
567c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LEN		4096
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate extern int key_setnet_g();
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static void logout_curr_key();
617c478bd9Sstevel@tonic-gate static int mkrootkey;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static char *sec_domain = NULL;
647c478bd9Sstevel@tonic-gate static char local_domain[MAXNETNAMELEN + 1];
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * fgets is broken in that if it reads a NUL character it will always return
687c478bd9Sstevel@tonic-gate  * EOF.  This replacement can deal with NULs
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate static char *
717c478bd9Sstevel@tonic-gate fgets_ignorenul(char *s, int n, FILE *stream)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	int fildes = fileno(stream);
747c478bd9Sstevel@tonic-gate 	int i = 0;
757c478bd9Sstevel@tonic-gate 	int rs = 0;
767c478bd9Sstevel@tonic-gate 	char c;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (fildes < 0)
797c478bd9Sstevel@tonic-gate 		return (NULL);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	while (i < n - 1) {
827c478bd9Sstevel@tonic-gate 		rs = read(fildes, &c, 1);
837c478bd9Sstevel@tonic-gate 		switch (rs) {
847c478bd9Sstevel@tonic-gate 		case 1:
857c478bd9Sstevel@tonic-gate 			break;
867c478bd9Sstevel@tonic-gate 		case 0:
877c478bd9Sstevel@tonic-gate 			/* EOF */
887c478bd9Sstevel@tonic-gate 			if (i > 0)
897c478bd9Sstevel@tonic-gate 				s[i] = '\0';
907c478bd9Sstevel@tonic-gate 			return (NULL);
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 		default:
937c478bd9Sstevel@tonic-gate 			return (NULL);
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 		switch (c) {
967c478bd9Sstevel@tonic-gate 		case '\0':
977c478bd9Sstevel@tonic-gate 			break;
987c478bd9Sstevel@tonic-gate 		case '\n':
997c478bd9Sstevel@tonic-gate 			s[i] = c;
1007c478bd9Sstevel@tonic-gate 			s[++i] = '\0';
1017c478bd9Sstevel@tonic-gate 			return (s);
1027c478bd9Sstevel@tonic-gate 		default:
1037c478bd9Sstevel@tonic-gate 		if (c != '\0')
1047c478bd9Sstevel@tonic-gate 			s[i++] = c;
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	s[i] = '\0';
1087c478bd9Sstevel@tonic-gate 	return (s);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /* write unencrypted secret key into root key file */
1137c478bd9Sstevel@tonic-gate static void
1147c478bd9Sstevel@tonic-gate write_rootkey(char *secret, char *flavor, keylen_t keylen, algtype_t algtype)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	char		line[MAXROOTKEY_LINE_LEN];
1177c478bd9Sstevel@tonic-gate 	char		keyent[MAXROOTKEY_LEN];
1187c478bd9Sstevel@tonic-gate 	algtype_t	atent;
1197c478bd9Sstevel@tonic-gate 	int		rootfd, bakfd, hexkeybytes;
1207c478bd9Sstevel@tonic-gate 	bool_t		lineone = TRUE;
1217c478bd9Sstevel@tonic-gate 	bool_t		gotit = FALSE;
1227c478bd9Sstevel@tonic-gate 	FILE		*rootfile, *bakfile;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
1257c478bd9Sstevel@tonic-gate 	if ((rename(ROOTKEY_FILE, ROOTKEY_FILE_BACKUP)) < 0) {
1267c478bd9Sstevel@tonic-gate 		if ((bakfd = creat(ROOTKEY_FILE_BACKUP, 0600)) < 0) {
1277c478bd9Sstevel@tonic-gate 			perror("Could not create /etc/.rootkey.bak");
1287c478bd9Sstevel@tonic-gate 			goto rootkey_err;
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		close(bakfd);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if ((rootfd = open(ROOTKEY_FILE, O_WRONLY+O_CREAT, 0600)) < 0) {
1347c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
1357c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1367c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
1377c478bd9Sstevel@tonic-gate 		(void) rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
1387c478bd9Sstevel@tonic-gate 		goto rootkey_err;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	if (!(rootfile = fdopen(rootfd, "w"))) {
1417c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
1427c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1437c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
1447c478bd9Sstevel@tonic-gate 		close(rootfd);
1457c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
1467c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
1477c478bd9Sstevel@tonic-gate 		goto rootkey_err;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	if (!(bakfile = fopen(ROOTKEY_FILE_BACKUP, "r"))) {
1507c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey.bak for reading");
1517c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1527c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
1537c478bd9Sstevel@tonic-gate 		(void) fclose(rootfile);
1547c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
1557c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
1567c478bd9Sstevel@tonic-gate 		goto rootkey_err;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	hexkeybytes = ((keylen + 7) / 8) * 2;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	while (fgets_ignorenul(line, MAXROOTKEY_LINE_LEN, bakfile)) {
162*a0368f78Speteh 		if (sscanf(line, "%s %d", keyent, &atent) < 2) {
163*a0368f78Speteh 			/*
164*a0368f78Speteh 			 * No encryption algorithm found in the file
165*a0368f78Speteh 			 * (atent) so default to DES.
166*a0368f78Speteh 			 */
167*a0368f78Speteh 			atent = AUTH_DES_ALGTYPE;
168*a0368f78Speteh 		}
1697c478bd9Sstevel@tonic-gate 		/*
1707c478bd9Sstevel@tonic-gate 		 * 192-bit keys always go on the first line
1717c478bd9Sstevel@tonic-gate 		 */
1727c478bd9Sstevel@tonic-gate 		if (lineone) {
1737c478bd9Sstevel@tonic-gate 			lineone = FALSE;
1747c478bd9Sstevel@tonic-gate 			if (keylen == 192) {
1757c478bd9Sstevel@tonic-gate 				gotit = TRUE;
1767c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s\n", secret);
1777c478bd9Sstevel@tonic-gate 			} else
1787c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
1797c478bd9Sstevel@tonic-gate 			(void) fflush(rootfile);
1807c478bd9Sstevel@tonic-gate 		} else {
1817c478bd9Sstevel@tonic-gate 			if ((strlen(keyent) == hexkeybytes) &&
1827c478bd9Sstevel@tonic-gate 			    (atent == algtype)) {
1837c478bd9Sstevel@tonic-gate 				/*
1847c478bd9Sstevel@tonic-gate 				 * Silently remove lines with the same
1857c478bd9Sstevel@tonic-gate 				 * keylen/algtype
1867c478bd9Sstevel@tonic-gate 				 */
1877c478bd9Sstevel@tonic-gate 				if (gotit)
1887c478bd9Sstevel@tonic-gate 					continue;
1897c478bd9Sstevel@tonic-gate 				else
1907c478bd9Sstevel@tonic-gate 					gotit = TRUE;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s %d\n", secret, algtype);
1937c478bd9Sstevel@tonic-gate 			} else
1947c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
1957c478bd9Sstevel@tonic-gate 			(void) fflush(rootfile);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* Append key to rootkey file */
2007c478bd9Sstevel@tonic-gate 	if (!gotit) {
2017c478bd9Sstevel@tonic-gate 		if (keylen == 192)
2027c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s\n", secret);
2037c478bd9Sstevel@tonic-gate 		else {
2047c478bd9Sstevel@tonic-gate 			if (lineone)
2057c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "\n");
2067c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s %d\n", secret, algtype);
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 	(void) fflush(rootfile);
2107c478bd9Sstevel@tonic-gate 	fclose(rootfile);
2117c478bd9Sstevel@tonic-gate 	fclose(bakfile);
2127c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
2137c478bd9Sstevel@tonic-gate 	if (keylen == 192)
2147c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Wrote secret key into %s\n", ROOTKEY_FILE);
2157c478bd9Sstevel@tonic-gate 	else
2167c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Wrote %s key into %s\n", flavor,
2177c478bd9Sstevel@tonic-gate 			ROOTKEY_FILE);
2187c478bd9Sstevel@tonic-gate 	return;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate rootkey_err:
2217c478bd9Sstevel@tonic-gate 	fprintf(stderr, "WARNING: Could not write %s key to /etc/.rootkey\n",
2227c478bd9Sstevel@tonic-gate 		flavor);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* Perform AUTH_DES keylogin */
2267c478bd9Sstevel@tonic-gate static int
2277c478bd9Sstevel@tonic-gate oldkeylogin(char *fullname, char *pass)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	char			secret[HEXKEYBYTES+1];
2307c478bd9Sstevel@tonic-gate 	struct key_netstarg	netst;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (getsecretkey(fullname, secret, pass) == 0) {
2337c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not find %s's secret key\n",
2347c478bd9Sstevel@tonic-gate 				fullname);
2357c478bd9Sstevel@tonic-gate 			if (sec_domain && *sec_domain &&
2367c478bd9Sstevel@tonic-gate 				strcasecmp(sec_domain, local_domain)) {
2377c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2387c478bd9Sstevel@tonic-gate "The system default domain '%s' is different from the Secure RPC\n\
2397c478bd9Sstevel@tonic-gate domain %s where the key is stored.  The Secure RPC domainname is\n\
2407c478bd9Sstevel@tonic-gate defined by the directory object stored in the /var/nis/NIS_COLD_START file.\n\
2417c478bd9Sstevel@tonic-gate If you need to change this Secure RPC domainname, please use the nisinit(1M)\n\
2427c478bd9Sstevel@tonic-gate command with the `-k` option.\n", local_domain, sec_domain);
2437c478bd9Sstevel@tonic-gate 			} else {
2447c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2457c478bd9Sstevel@tonic-gate 		"Make sure the secret key is stored in domain %s\n",
2467c478bd9Sstevel@tonic-gate 				local_domain);
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate 			return (1);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		if (secret[0] == 0) {
2527c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Password incorrect for %s\n",
2537c478bd9Sstevel@tonic-gate 				fullname);
2547c478bd9Sstevel@tonic-gate 			return (1);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 		/* revoke any existing (lingering) credentials... */
2577c478bd9Sstevel@tonic-gate 		logout_curr_key();
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		memcpy(netst.st_priv_key, secret, HEXKEYBYTES);
2607c478bd9Sstevel@tonic-gate 		memset(secret, 0, HEXKEYBYTES);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		netst.st_pub_key[0] = 0;
2637c478bd9Sstevel@tonic-gate 		netst.st_netname = strdup(fullname);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		/* do actual key login */
2667c478bd9Sstevel@tonic-gate 		if (key_setnet(&netst) < 0) {
2677c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not set %s's secret key\n",
2687c478bd9Sstevel@tonic-gate 				fullname);
2697c478bd9Sstevel@tonic-gate 			fprintf(stderr, "May be the keyserv is down?\n");
2707c478bd9Sstevel@tonic-gate 			if (mkrootkey == 0)   /* nothing else to do */
2717c478bd9Sstevel@tonic-gate 				return (1);
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		/* write unencrypted secret key into root key file */
2757c478bd9Sstevel@tonic-gate 		if (mkrootkey)
2767c478bd9Sstevel@tonic-gate 			write_rootkey(netst.st_priv_key, "des", 192, 0);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		return (0);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate  * Revokes the existing credentials for Secure-RPC and Secure-NFS.
2837c478bd9Sstevel@tonic-gate  * This should only be called if the user entered the correct password;
2847c478bd9Sstevel@tonic-gate  * sorta like the way "su" doesn't force a login if you enter the wrong
2857c478bd9Sstevel@tonic-gate  * password.
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static void
2897c478bd9Sstevel@tonic-gate logout_curr_key()
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	static char		secret[HEXKEYBYTES + 1];
2927c478bd9Sstevel@tonic-gate 	struct nfs_revauth_args	nra;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * try to revoke the existing key/credentials, assuming
2967c478bd9Sstevel@tonic-gate 	 * one exists.  this will effectively mark "stale" any
2977c478bd9Sstevel@tonic-gate 	 * cached credientials...
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if (key_setsecret(secret) < 0) {
3007c478bd9Sstevel@tonic-gate 		return;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * it looks like a credential already existed, so try and
3057c478bd9Sstevel@tonic-gate 	 * revoke any lingering Secure-NFS privledges.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	nra.authtype = AUTH_DES;
3097c478bd9Sstevel@tonic-gate 	nra.uid = getuid();
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	(void) _nfssys(NFS_REVAUTH, &nra);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate void
3157c478bd9Sstevel@tonic-gate usage(cmd)
3167c478bd9Sstevel@tonic-gate 	char *cmd;
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	fprintf(stderr, "usage: %s [-r]\n", cmd);
3197c478bd9Sstevel@tonic-gate 	exit(1);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 
32349e7ca49Speteh int
32449e7ca49Speteh main(int argc, char *argv[])
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	char		secret[4096];
3277c478bd9Sstevel@tonic-gate 	char		fullname[MAXNETNAMELEN + 1];
3287c478bd9Sstevel@tonic-gate 	char		*getpass();
3297c478bd9Sstevel@tonic-gate 	char		*pass;
3307c478bd9Sstevel@tonic-gate 	int		i = 0;
3317c478bd9Sstevel@tonic-gate 	mechanism_t	**mechlist;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (argc == 1)
3347c478bd9Sstevel@tonic-gate 		mkrootkey = 0;
3357c478bd9Sstevel@tonic-gate 	else if (argc == 2 && (strcmp(argv[1], "-r") == 0)) {
3367c478bd9Sstevel@tonic-gate 		if (geteuid() != 0) {
3377c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Must be root to use -r option.\n");
3387c478bd9Sstevel@tonic-gate 			exit(1);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		mkrootkey = 1;
3417c478bd9Sstevel@tonic-gate 	} else
3427c478bd9Sstevel@tonic-gate 		usage(argv[0]);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (getnetname(fullname) == 0) {
3457c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Could not generate netname\n");
3467c478bd9Sstevel@tonic-gate 		exit(1);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	sec_domain = strdup(strchr(fullname, '@') + 1);
3497c478bd9Sstevel@tonic-gate 	getdomainname(local_domain, MAXNETNAMELEN);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (!(pass = getpass("Password:")))
3527c478bd9Sstevel@tonic-gate 		exit(1);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (mechlist = __nis_get_mechanisms(FALSE)) {
3557c478bd9Sstevel@tonic-gate 		while (mechlist[i]) {
3567c478bd9Sstevel@tonic-gate 			char		*alias;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 			if (AUTH_DES_COMPAT_CHK(mechlist[i])) {
3597c478bd9Sstevel@tonic-gate 				(void) oldkeylogin(fullname, pass);
3607c478bd9Sstevel@tonic-gate 				i++;
3617c478bd9Sstevel@tonic-gate 				continue;
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			if (VALID_ALIAS(mechlist[i]->alias))
3657c478bd9Sstevel@tonic-gate 				alias = mechlist[i]->alias;
3667c478bd9Sstevel@tonic-gate 			else
3677c478bd9Sstevel@tonic-gate 				alias = "";
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 			if (getsecretkey_g(fullname, mechlist[i]->keylen,
3707c478bd9Sstevel@tonic-gate 						mechlist[i]->algtype, secret,
3717c478bd9Sstevel@tonic-gate 						(((mechlist[i]->keylen / 7) +
3727c478bd9Sstevel@tonic-gate 						8) * 2) + 1, pass) == 0) {
3737c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3747c478bd9Sstevel@tonic-gate 				"WARNING: Could not find %s's %s secret key\n",
3757c478bd9Sstevel@tonic-gate 					fullname, alias);
3767c478bd9Sstevel@tonic-gate 				i++;
3777c478bd9Sstevel@tonic-gate 				continue;
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 			if (secret[0] == 0) {
3817c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3827c478bd9Sstevel@tonic-gate 				    "Password incorrect for %s's %s key.\n",
3837c478bd9Sstevel@tonic-gate 					fullname, alias);
3847c478bd9Sstevel@tonic-gate 				i++;
3857c478bd9Sstevel@tonic-gate 				continue;
3867c478bd9Sstevel@tonic-gate 			}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 			if (key_setnet_g(fullname, secret,
3897c478bd9Sstevel@tonic-gate 						mechlist[i]->keylen, NULL, 0,
3907c478bd9Sstevel@tonic-gate 						mechlist[i]->algtype) < 0) {
3917c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3927c478bd9Sstevel@tonic-gate 				"Could not set %s's %s secret key\n",
3937c478bd9Sstevel@tonic-gate 					fullname, alias);
3947c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3957c478bd9Sstevel@tonic-gate 					"May be the keyserv is down?\n");
3967c478bd9Sstevel@tonic-gate 				exit(1);
3977c478bd9Sstevel@tonic-gate 			}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 			if (mkrootkey)
4007c478bd9Sstevel@tonic-gate 				write_rootkey(secret, mechlist[i]->alias,
4017c478bd9Sstevel@tonic-gate 						mechlist[i]->keylen,
4027c478bd9Sstevel@tonic-gate 						mechlist[i]->algtype);
4037c478bd9Sstevel@tonic-gate 			i++;
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 	} else
4067c478bd9Sstevel@tonic-gate 		exit(oldkeylogin(fullname, pass));
4077c478bd9Sstevel@tonic-gate 
40849e7ca49Speteh 	return (0);
4097c478bd9Sstevel@tonic-gate }
410