xref: /illumos-gate/usr/src/cmd/gss/gssd/gssdtest.c (revision 694c35faa87b858ecdadfe4fc592615f4eefbb07)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Test client for gssd.  This program is not shipped on the binary
297c478bd9Sstevel@tonic-gate  * release.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
377c478bd9Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
387c478bd9Sstevel@tonic-gate #include "gssd.h"
397c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	_KERNEL
427c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
437c478bd9Sstevel@tonic-gate #undef	_KERNEL
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate int gss_major_code;
467c478bd9Sstevel@tonic-gate int gss_minor_code;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int init_sec_context_phase = 0;
497c478bd9Sstevel@tonic-gate int accept_sec_context_phase = 0;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate gss_ctx_id_t    initiator_context_handle;
527c478bd9Sstevel@tonic-gate gss_ctx_id_t    acceptor_context_handle;
537c478bd9Sstevel@tonic-gate gss_cred_id_t   acceptor_credentials;
547c478bd9Sstevel@tonic-gate gss_buffer_desc init_token_buffer;
557c478bd9Sstevel@tonic-gate gss_buffer_desc accept_token_buffer;
567c478bd9Sstevel@tonic-gate gss_buffer_desc delete_token_buffer;
577c478bd9Sstevel@tonic-gate gss_buffer_desc message_buffer;
587c478bd9Sstevel@tonic-gate gss_buffer_desc msg_token;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	LOOP_COUNTER  100
617c478bd9Sstevel@tonic-gate #define	GSS_KRB5_MECH_OID "1.2.840.113554.1.2.2"
627c478bd9Sstevel@tonic-gate #define	GSS_DUMMY_MECH_OID "1.3.6.1.4.1.42.2.26.1.2"
637c478bd9Sstevel@tonic-gate #ifdef _KERNEL
647c478bd9Sstevel@tonic-gate #define	OCTAL_MACRO "%03o."
657c478bd9Sstevel@tonic-gate #define	MALLOC(n) kmem_alloc((n), KM_SLEEP)
667c478bd9Sstevel@tonic-gate #define	CALLOC(n, s) kmem_zalloc((n)*(s), KM_SLEEP)
677c478bd9Sstevel@tonic-gate #define	FREE(x, n) kmem_free((x), (n))
687c478bd9Sstevel@tonic-gate #define	memcpy(dst, src, n) bcopy((src), (dst), (n))
697c478bd9Sstevel@tonic-gate #define	fprintf(s, m) printf(m)
707c478bd9Sstevel@tonic-gate #define	isspace(s) ((s) == ' ' || (s) == '\t' || (s) == '\n' || \
717c478bd9Sstevel@tonic-gate 		(s) == '\r' || (s) == '\v' || (s) == '\f')
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static char *strdup(const char *s)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	int len = strlen(s);
767c478bd9Sstevel@tonic-gate 	char *new = MALLOC(len+1);
777c478bd9Sstevel@tonic-gate 	strcpy(new, s);
787c478bd9Sstevel@tonic-gate 	return (new);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #else /* !_KERNEL */
827c478bd9Sstevel@tonic-gate #define	OCTAL_MACRO "%03.3o."
837c478bd9Sstevel@tonic-gate #define	MALLOC(n) malloc(n)
847c478bd9Sstevel@tonic-gate #define	CALLOC(n, s) calloc((n), (s))
857c478bd9Sstevel@tonic-gate #define	FREE(x, n) free(x)
867c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static gss_OID gss_str2oid(char *);
897c478bd9Sstevel@tonic-gate static char * gss_oid2str(gss_OID);
907c478bd9Sstevel@tonic-gate static void instructs();
917c478bd9Sstevel@tonic-gate static void usage();
927c478bd9Sstevel@tonic-gate static int parse_input_line(char *, int *, char ***);
937c478bd9Sstevel@tonic-gate extern uid_t getuid();
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static void _gss_init_sec_context(int, char **);
967c478bd9Sstevel@tonic-gate static void _gss_acquire_cred(int, char **);
977c478bd9Sstevel@tonic-gate static void _gss_add_cred(int, char **);
987c478bd9Sstevel@tonic-gate static void _gss_sign(int, char **);
997c478bd9Sstevel@tonic-gate static void _gss_release_cred(int, char **);
1007c478bd9Sstevel@tonic-gate static void _gss_accept_sec_context(int, char **);
1017c478bd9Sstevel@tonic-gate static void _gss_process_context_token(int, char **);
1027c478bd9Sstevel@tonic-gate static void _gss_delete_sec_context(int, char **);
1037c478bd9Sstevel@tonic-gate static void _gss_context_time(int, char **);
1047c478bd9Sstevel@tonic-gate static void _gss_verify(int, char **);
1057c478bd9Sstevel@tonic-gate static void _gss_seal(int, char **);
1067c478bd9Sstevel@tonic-gate static void _gss_unseal(int, char **);
1077c478bd9Sstevel@tonic-gate static void _gss_display_status(int, char **);
1087c478bd9Sstevel@tonic-gate static void _gss_indicate_mechs(int, char **);
1097c478bd9Sstevel@tonic-gate static void _gss_inquire_cred(int, char **);
1107c478bd9Sstevel@tonic-gate static void _gssd_expname_to_unix_cred(int, char **);
1117c478bd9Sstevel@tonic-gate static void _gssd_name_to_unix_cred(int, char **);
1127c478bd9Sstevel@tonic-gate static void _gssd_get_group_info(int, char **);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static int do_gssdtest(char *buf);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #ifndef _KERNEL
1187c478bd9Sstevel@tonic-gate static int read_line(char *buf, int size)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int len;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/* read the next line. If cntl-d, return with zero char count */
1237c478bd9Sstevel@tonic-gate 	printf(gettext("\n> "));
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (fgets(buf, size, stdin) == NULL)
1267c478bd9Sstevel@tonic-gate 		return (0);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	len = strlen(buf);
1297c478bd9Sstevel@tonic-gate 	buf[--len] = '\0';
1307c478bd9Sstevel@tonic-gate 	return (len);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate int
1347c478bd9Sstevel@tonic-gate main()
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	char buf[512];
1377c478bd9Sstevel@tonic-gate 	int len, ret;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* Print out usage and instructions to start off the session */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	instructs();
1427c478bd9Sstevel@tonic-gate 	usage();
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Loop, repeatedly calling parse_input_line() to get the
1467c478bd9Sstevel@tonic-gate 	 * next line and parse it into argc and argv. Act on the
1477c478bd9Sstevel@tonic-gate 	 * arguements found on the line.
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	do {
1517c478bd9Sstevel@tonic-gate 		len = read_line(buf, 512);
1527c478bd9Sstevel@tonic-gate 		if (len)
1537c478bd9Sstevel@tonic-gate 			ret = do_gssdtest(buf);
1547c478bd9Sstevel@tonic-gate 	} while (len && !ret);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	return (0);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate #endif /* !_KERNEL */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static int
1617c478bd9Sstevel@tonic-gate do_gssdtest(char *buf)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	int argc, seal_argc;
1647c478bd9Sstevel@tonic-gate 	int i;
1657c478bd9Sstevel@tonic-gate 	char **argv, **argv_array;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	char *cmd;
1687c478bd9Sstevel@tonic-gate 	char *seal_ini_array [] = { "initiator", " Hello"};
1697c478bd9Sstevel@tonic-gate 	char *seal_acc_array [] = { "acceptor", " Hello"};
1707c478bd9Sstevel@tonic-gate 	char *unseal_acc_array [] = {"acceptor"};
1717c478bd9Sstevel@tonic-gate 	char *unseal_ini_array [] = {"initiator"};
1727c478bd9Sstevel@tonic-gate 	char *delet_acc_array [] = {"acceptor"};
1737c478bd9Sstevel@tonic-gate 	char *delet_ini_array [] = {"initiator"};
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	argv = 0;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (parse_input_line(buf, &argc, &argv) == 0) {
1787c478bd9Sstevel@tonic-gate 		printf(gettext("\n"));
1797c478bd9Sstevel@tonic-gate 		return (1);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (argc == 0) {
1837c478bd9Sstevel@tonic-gate 		usage();
1847c478bd9Sstevel@tonic-gate 		/*LINTED*/
1857c478bd9Sstevel@tonic-gate 		FREE(argv_array, (argc+1)*sizeof (char *));
1867c478bd9Sstevel@tonic-gate 		return (0);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * remember argv_array address, which is memory calloc'd by
1917c478bd9Sstevel@tonic-gate 	 * parse_input_line, so it can be free'd at the end of the loop.
1927c478bd9Sstevel@tonic-gate 	 */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	argv_array = argv;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	cmd = argv[0];
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	argc--;
1997c478bd9Sstevel@tonic-gate 	argv++;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (strcmp(cmd, "gss_loop") == 0 ||
2027c478bd9Sstevel@tonic-gate 	    strcmp(cmd, "loop") == 0) {
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		if (argc < 1) {
2057c478bd9Sstevel@tonic-gate 			usage();
2067c478bd9Sstevel@tonic-gate 			FREE(argv_array, (argc+2) * sizeof (char *));
2077c478bd9Sstevel@tonic-gate 			return (0);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 		for (i = 0; i < LOOP_COUNTER; i++) {
2107c478bd9Sstevel@tonic-gate 			printf(gettext("Loop Count is %d \n"), i);
2117c478bd9Sstevel@tonic-gate 			/*
2127c478bd9Sstevel@tonic-gate 			 * if (i > 53)
2137c478bd9Sstevel@tonic-gate 			 * 	printf ("Loop counter is greater than 55\n");
2147c478bd9Sstevel@tonic-gate 			 */
2157c478bd9Sstevel@tonic-gate 			_gss_acquire_cred(argc, argv);
2167c478bd9Sstevel@tonic-gate 			_gss_init_sec_context(argc, argv);
2177c478bd9Sstevel@tonic-gate 			_gss_accept_sec_context(0, argv);
2187c478bd9Sstevel@tonic-gate 			_gss_init_sec_context(argc, argv);
219*694c35faSJosef 'Jeff' Sipek 
2207c478bd9Sstevel@tonic-gate 			seal_argc = 2;
2217c478bd9Sstevel@tonic-gate 			_gss_seal(seal_argc, seal_ini_array);
2227c478bd9Sstevel@tonic-gate 			seal_argc = 1;
2237c478bd9Sstevel@tonic-gate 			_gss_unseal(seal_argc, unseal_acc_array);
2247c478bd9Sstevel@tonic-gate 			seal_argc = 2;
2257c478bd9Sstevel@tonic-gate 			_gss_seal(seal_argc, seal_acc_array);
2267c478bd9Sstevel@tonic-gate 			seal_argc = 1;
2277c478bd9Sstevel@tonic-gate 			_gss_unseal(seal_argc, unseal_ini_array);
2287c478bd9Sstevel@tonic-gate 			seal_argc = 2;
2297c478bd9Sstevel@tonic-gate 			_gss_sign(seal_argc, seal_ini_array);
2307c478bd9Sstevel@tonic-gate 			seal_argc = 1;
2317c478bd9Sstevel@tonic-gate 			_gss_verify(seal_argc, unseal_acc_array);
2327c478bd9Sstevel@tonic-gate 			seal_argc = 2;
2337c478bd9Sstevel@tonic-gate 			_gss_sign(seal_argc, seal_acc_array);
2347c478bd9Sstevel@tonic-gate 			seal_argc = 1;
2357c478bd9Sstevel@tonic-gate 			_gss_verify(seal_argc, unseal_ini_array);
2367c478bd9Sstevel@tonic-gate 			_gss_delete_sec_context(argc, delet_acc_array);
2377c478bd9Sstevel@tonic-gate 			_gss_delete_sec_context(argc, delet_ini_array);
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	if (strcmp(cmd, "gss_all") == 0 ||
2417c478bd9Sstevel@tonic-gate 	    strcmp(cmd, "all") == 0) {
2427c478bd9Sstevel@tonic-gate 		_gss_acquire_cred(argc, argv);
2437c478bd9Sstevel@tonic-gate 		_gss_init_sec_context(argc, argv);
2447c478bd9Sstevel@tonic-gate 		_gss_accept_sec_context(0, argv);
2457c478bd9Sstevel@tonic-gate 		_gss_init_sec_context(argc, argv);
246*694c35faSJosef 'Jeff' Sipek 
2477c478bd9Sstevel@tonic-gate 		seal_argc = 2;
2487c478bd9Sstevel@tonic-gate 		_gss_seal(seal_argc, seal_acc_array);
2497c478bd9Sstevel@tonic-gate 		seal_argc = 1;
2507c478bd9Sstevel@tonic-gate 		_gss_unseal(seal_argc, unseal_ini_array);
2517c478bd9Sstevel@tonic-gate 		seal_argc = 2;
2527c478bd9Sstevel@tonic-gate 		_gss_seal(seal_argc, seal_ini_array);
2537c478bd9Sstevel@tonic-gate 		seal_argc = 1;
2547c478bd9Sstevel@tonic-gate 		_gss_unseal(seal_argc, unseal_acc_array);
2557c478bd9Sstevel@tonic-gate 		seal_argc = 2;
2567c478bd9Sstevel@tonic-gate 		_gss_sign(seal_argc, seal_ini_array);
2577c478bd9Sstevel@tonic-gate 		seal_argc = 1;
2587c478bd9Sstevel@tonic-gate 		_gss_verify(seal_argc, unseal_acc_array);
2597c478bd9Sstevel@tonic-gate 		seal_argc = 2;
2607c478bd9Sstevel@tonic-gate 		_gss_sign(seal_argc, seal_acc_array);
2617c478bd9Sstevel@tonic-gate 		seal_argc = 1;
2627c478bd9Sstevel@tonic-gate 		_gss_verify(seal_argc, unseal_ini_array);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	if (strcmp(cmd, "gss_acquire_cred") == 0 ||
2667c478bd9Sstevel@tonic-gate 	    strcmp(cmd, "acquire") == 0) {
2677c478bd9Sstevel@tonic-gate 		_gss_acquire_cred(argc, argv);
2687c478bd9Sstevel@tonic-gate 		if (argc == 1)
2697c478bd9Sstevel@tonic-gate 			_gss_add_cred(argc, argv);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_release_cred") == 0 ||
2737c478bd9Sstevel@tonic-gate 		strcmp(cmd, "release") == 0)
2747c478bd9Sstevel@tonic-gate 		_gss_release_cred(argc, argv);
2757c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_init_sec_context") == 0 ||
2767c478bd9Sstevel@tonic-gate 		strcmp(cmd, "init") == 0)
2777c478bd9Sstevel@tonic-gate 		_gss_init_sec_context(argc, argv);
2787c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_accept_sec_context") == 0 ||
2797c478bd9Sstevel@tonic-gate 		strcmp(cmd, "accept") == 0)
2807c478bd9Sstevel@tonic-gate 		_gss_accept_sec_context(argc, argv);
2817c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_process_context_token") == 0 ||
2827c478bd9Sstevel@tonic-gate 		strcmp(cmd, "process") == 0)
2837c478bd9Sstevel@tonic-gate 		_gss_process_context_token(argc, argv);
2847c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_delete_sec_context") == 0 ||
2857c478bd9Sstevel@tonic-gate 		strcmp(cmd, "delete") == 0)
2867c478bd9Sstevel@tonic-gate 		_gss_delete_sec_context(argc, argv);
2877c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_context_time") == 0 ||
2887c478bd9Sstevel@tonic-gate 		strcmp(cmd, "time") == 0)
2897c478bd9Sstevel@tonic-gate 		_gss_context_time(argc, argv);
2907c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_sign") == 0 ||
2917c478bd9Sstevel@tonic-gate 		strcmp(cmd, "sign") == 0)
2927c478bd9Sstevel@tonic-gate 		_gss_sign(argc, argv);
2937c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_verify") == 0 ||
2947c478bd9Sstevel@tonic-gate 		strcmp(cmd, "verify") == 0)
2957c478bd9Sstevel@tonic-gate 		_gss_verify(argc, argv);
2967c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_seal") == 0 ||
2977c478bd9Sstevel@tonic-gate 		strcmp(cmd, "seal") == 0)
2987c478bd9Sstevel@tonic-gate 		_gss_seal(argc, argv);
2997c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_unseal") == 0 ||
3007c478bd9Sstevel@tonic-gate 		strcmp(cmd, "unseal") == 0)
3017c478bd9Sstevel@tonic-gate 		_gss_unseal(argc, argv);
3027c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_display_status") == 0||
3037c478bd9Sstevel@tonic-gate 		strcmp(cmd, "status") == 0)
3047c478bd9Sstevel@tonic-gate 		_gss_display_status(argc, argv);
3057c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_indicate_mechs") == 0 ||
3067c478bd9Sstevel@tonic-gate 		strcmp(cmd, "indicate") == 0)
3077c478bd9Sstevel@tonic-gate 		_gss_indicate_mechs(argc, argv);
3087c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "gss_inquire_cred") == 0 ||
3097c478bd9Sstevel@tonic-gate 		strcmp(cmd, "inquire") == 0)
3107c478bd9Sstevel@tonic-gate 		_gss_inquire_cred(argc, argv);
3117c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "expname2unixcred") == 0 ||
3127c478bd9Sstevel@tonic-gate 		strcmp(cmd, "gsscred_expname_to_unix_cred") == 0)
3137c478bd9Sstevel@tonic-gate 		_gssd_expname_to_unix_cred(argc, argv);
3147c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "name2unixcred") == 0 ||
3157c478bd9Sstevel@tonic-gate 		strcmp(cmd, "gsscred_name_to_unix_cred") == 0)
3167c478bd9Sstevel@tonic-gate 		_gssd_name_to_unix_cred(argc, argv);
3177c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "grpinfo") == 0 ||
3187c478bd9Sstevel@tonic-gate 		strcmp(cmd, "gss_get_group_info") == 0)
3197c478bd9Sstevel@tonic-gate 		_gssd_get_group_info(argc, argv);
3207c478bd9Sstevel@tonic-gate 	else if (strcmp(cmd, "exit") == 0) {
3217c478bd9Sstevel@tonic-gate 		printf(gettext("\n"));
3227c478bd9Sstevel@tonic-gate 		FREE(argv_array, (argc+2) * sizeof (char *));
3237c478bd9Sstevel@tonic-gate 		return (1);
3247c478bd9Sstevel@tonic-gate 	} else
3257c478bd9Sstevel@tonic-gate 		usage();
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/* free argv array */
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	FREE(argv_array, (argc+2) * sizeof (char *));
3307c478bd9Sstevel@tonic-gate 	return (0);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate static void
3347c478bd9Sstevel@tonic-gate _gss_acquire_cred(argc, argv)
3357c478bd9Sstevel@tonic-gate int argc;
3367c478bd9Sstevel@tonic-gate char **argv;
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	OM_UINT32 status, minor_status;
3407c478bd9Sstevel@tonic-gate 	gss_buffer_desc name;
3417c478bd9Sstevel@tonic-gate 	gss_name_t desired_name = (gss_name_t) 0;
3427c478bd9Sstevel@tonic-gate 	OM_uint32 time_req;
3437c478bd9Sstevel@tonic-gate 	gss_OID_set_desc desired_mechs_desc;
3447c478bd9Sstevel@tonic-gate 	gss_OID_set desired_mechs = &desired_mechs_desc;
3457c478bd9Sstevel@tonic-gate 	int cred_usage;
3467c478bd9Sstevel@tonic-gate 	gss_OID_set actual_mechs = GSS_C_NULL_OID_SET;
3477c478bd9Sstevel@tonic-gate 	gss_OID_set inquire_mechs = GSS_C_NULL_OID_SET;
3487c478bd9Sstevel@tonic-gate 	OM_UINT32 time_rec;
3497c478bd9Sstevel@tonic-gate 	char * string;
3507c478bd9Sstevel@tonic-gate 	char * inq_string;
3517c478bd9Sstevel@tonic-gate 	uid_t uid;
3527c478bd9Sstevel@tonic-gate 	gss_OID mech_type;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * First set up the command line independent input arguments.
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	time_req = (OM_uint32) 0;
3597c478bd9Sstevel@tonic-gate 	cred_usage = GSS_C_ACCEPT;
3607c478bd9Sstevel@tonic-gate 	uid = getuid();
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* Parse the command line for the variable input arguments */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (argc == 0) {
3657c478bd9Sstevel@tonic-gate 		usage();
3667c478bd9Sstevel@tonic-gate 		return;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * Get the name of the principal.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	name.length = strlen(argv[0])+1;
3747c478bd9Sstevel@tonic-gate 	name.value = argv[0];
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * Now convert the string given by the first argument into internal
3787c478bd9Sstevel@tonic-gate 	 * form suitable for input to gss_acquire_cred()
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	if ((status = gss_import_name(&minor_status, &name,
3827c478bd9Sstevel@tonic-gate 		(gss_OID)GSS_C_NT_HOSTBASED_SERVICE, &desired_name))
3837c478bd9Sstevel@tonic-gate 		!= GSS_S_COMPLETE) {
3847c478bd9Sstevel@tonic-gate 		printf(gettext(
3857c478bd9Sstevel@tonic-gate 			"could not parse desired name: err (octal) %o (%s)\n"),
3867c478bd9Sstevel@tonic-gate 			status, gettext("gss_acquire_cred error"));
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	argc--;
3917c478bd9Sstevel@tonic-gate 	argv++;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * The next argument is an OID in dotted decimal form.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (argc == 0) {
3987c478bd9Sstevel@tonic-gate 		printf(gettext("Assuming Kerberos V5 as the mechanism\n"));
3997c478bd9Sstevel@tonic-gate 		printf(gettext(
4007c478bd9Sstevel@tonic-gate 			"The mech OID 1.2.840.113554.1.2.2 will be used\n"));
4017c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid((char *)GSS_KRB5_MECH_OID);
4027c478bd9Sstevel@tonic-gate 	} else
4037c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid(argv[0]);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (mech_type == 0 || mech_type->length == 0) {
4067c478bd9Sstevel@tonic-gate 		printf(gettext("improperly formated mechanism OID\n"));
4077c478bd9Sstevel@tonic-gate 		return;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * set up desired_mechs so it points to mech_type.
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	desired_mechs = (gss_OID_set) MALLOC(sizeof (gss_OID_desc));
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	desired_mechs->count = 1;
4177c478bd9Sstevel@tonic-gate 	desired_mechs->elements = mech_type;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	status = kgss_acquire_cred(
4207c478bd9Sstevel@tonic-gate 				&minor_status,
4217c478bd9Sstevel@tonic-gate 				desired_name,
4227c478bd9Sstevel@tonic-gate 				time_req,
4237c478bd9Sstevel@tonic-gate 				desired_mechs,
4247c478bd9Sstevel@tonic-gate 				cred_usage,
4257c478bd9Sstevel@tonic-gate 				&acceptor_credentials,
4267c478bd9Sstevel@tonic-gate 				&actual_mechs,
4277c478bd9Sstevel@tonic-gate 				&time_rec,
4287c478bd9Sstevel@tonic-gate 				uid);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	gss_major_code = status;
4337c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
4367c478bd9Sstevel@tonic-gate 		/* process returned values */
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		printf(gettext("\nacquire succeeded\n\n"));
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		/*
4417c478bd9Sstevel@tonic-gate 		 * print out the actual mechs returned  NB: Since only one
4427c478bd9Sstevel@tonic-gate 		 * mechanism is specified in desired_mechs, only one
4437c478bd9Sstevel@tonic-gate 		 * can be returned in actual_mechs. Consequently,
4447c478bd9Sstevel@tonic-gate 		 * actual_mechs->elements points to an array of only one
4457c478bd9Sstevel@tonic-gate 		 * element.
4467c478bd9Sstevel@tonic-gate 		 */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		if ((string = gss_oid2str(actual_mechs->elements)) == 0) {
4497c478bd9Sstevel@tonic-gate 			printf(gettext("actual mechs == NULL\n\n"));
4507c478bd9Sstevel@tonic-gate 		} else {
4517c478bd9Sstevel@tonic-gate 			printf(gettext("actual mechs  = %s\n\n"), string);
4527c478bd9Sstevel@tonic-gate 			FREE(string, (actual_mechs->elements->length+1)*4+1);
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		if (cred_usage == GSS_C_BOTH)
4567c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_BOTH\n\n"));
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (cred_usage == GSS_C_INITIATE)
4597c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_INITIATE\n\n"));
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		if (cred_usage == GSS_C_ACCEPT)
4627c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_ACCEPT\n\n"));
4637c478bd9Sstevel@tonic-gate 		status = kgss_inquire_cred(
4647c478bd9Sstevel@tonic-gate 				&minor_status,
4657c478bd9Sstevel@tonic-gate 				acceptor_credentials,
4667c478bd9Sstevel@tonic-gate 				NULL,
4677c478bd9Sstevel@tonic-gate 				&time_req,
4687c478bd9Sstevel@tonic-gate 				&cred_usage,
4697c478bd9Sstevel@tonic-gate 				&inquire_mechs,
4707c478bd9Sstevel@tonic-gate 				uid);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		if (status != GSS_S_COMPLETE)
4737c478bd9Sstevel@tonic-gate 			printf(gettext("server ret err (octal) %o (%s)\n"),
4747c478bd9Sstevel@tonic-gate 			status, gettext("gss_inquire_cred error"));
4757c478bd9Sstevel@tonic-gate 		else {
4767c478bd9Sstevel@tonic-gate 			if ((inq_string =
4777c478bd9Sstevel@tonic-gate 				gss_oid2str(inquire_mechs->elements)) == 0) {
4787c478bd9Sstevel@tonic-gate 				printf(gettext
4797c478bd9Sstevel@tonic-gate 					("mechs from inquire == NULL\n\n"));
4807c478bd9Sstevel@tonic-gate 			} else {
4817c478bd9Sstevel@tonic-gate 				printf(gettext
4827c478bd9Sstevel@tonic-gate 					("mechs from inquiry  = %s\n\n"),
4837c478bd9Sstevel@tonic-gate 					inq_string);
4847c478bd9Sstevel@tonic-gate 				FREE(inq_string,
4857c478bd9Sstevel@tonic-gate 				(inquire_mechs->elements->length+1)*4+1);
4867c478bd9Sstevel@tonic-gate 			}
4877c478bd9Sstevel@tonic-gate 			printf(gettext("inquire_cred successful \n\n"));
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	} else {
4917c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
4927c478bd9Sstevel@tonic-gate 			status, gettext("gss_acquire_cred error"));
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/* free allocated memory */
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/* actual mechs is allocated by clnt_stubs. Release it here */
4987c478bd9Sstevel@tonic-gate 	if (actual_mechs != GSS_C_NULL_OID_SET)
4997c478bd9Sstevel@tonic-gate 		gss_release_oid_set_and_oids(&minor_status, &actual_mechs);
5007c478bd9Sstevel@tonic-gate 	if (inquire_mechs != GSS_C_NULL_OID_SET)
5017c478bd9Sstevel@tonic-gate 		gss_release_oid_set_and_oids(&minor_status, &inquire_mechs);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	gss_release_name(&minor_status, &desired_name);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* mech_type and desired_mechs are allocated above. Release it here */
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	FREE(mech_type->elements, mech_type->length);
5087c478bd9Sstevel@tonic-gate 	FREE(mech_type, sizeof (gss_OID_desc));
5097c478bd9Sstevel@tonic-gate 	FREE(desired_mechs, sizeof (gss_OID_desc));
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static void
5137c478bd9Sstevel@tonic-gate _gss_add_cred(argc, argv)
5147c478bd9Sstevel@tonic-gate int argc;
5157c478bd9Sstevel@tonic-gate char **argv;
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	OM_UINT32 status, minor_status;
5197c478bd9Sstevel@tonic-gate 	gss_buffer_desc name;
5207c478bd9Sstevel@tonic-gate 	gss_name_t desired_name = (gss_name_t) 0;
5217c478bd9Sstevel@tonic-gate 	OM_uint32 time_req;
5227c478bd9Sstevel@tonic-gate 	OM_uint32 initiator_time_req;
5237c478bd9Sstevel@tonic-gate 	OM_uint32 acceptor_time_req;
5247c478bd9Sstevel@tonic-gate 	int cred_usage;
5257c478bd9Sstevel@tonic-gate 	gss_OID_set actual_mechs = GSS_C_NULL_OID_SET;
5267c478bd9Sstevel@tonic-gate 	gss_OID_set inquire_mechs = GSS_C_NULL_OID_SET;
5277c478bd9Sstevel@tonic-gate 	char * string;
5287c478bd9Sstevel@tonic-gate 	uid_t uid;
5297c478bd9Sstevel@tonic-gate 	gss_OID mech_type;
5307c478bd9Sstevel@tonic-gate 	int i;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * First set up the command line independent input arguments.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	initiator_time_req = (OM_uint32) 0;
5377c478bd9Sstevel@tonic-gate 	acceptor_time_req = (OM_uint32) 0;
5387c478bd9Sstevel@tonic-gate 	cred_usage = GSS_C_ACCEPT;
5397c478bd9Sstevel@tonic-gate 	uid = getuid();
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/* Parse the command line for the variable input arguments */
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (argc == 0) {
5447c478bd9Sstevel@tonic-gate 		usage();
5457c478bd9Sstevel@tonic-gate 		return;
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Get the name of the principal.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	name.length = strlen(argv[0])+1;
5537c478bd9Sstevel@tonic-gate 	name.value = argv[0];
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*
5567c478bd9Sstevel@tonic-gate 	 * Now convert the string given by the first argument into internal
5577c478bd9Sstevel@tonic-gate 	 * form suitable for input to gss_acquire_cred()
5587c478bd9Sstevel@tonic-gate 	 */
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if ((status = gss_import_name(&minor_status, &name,
5617c478bd9Sstevel@tonic-gate 		(gss_OID)GSS_C_NT_HOSTBASED_SERVICE, &desired_name))
5627c478bd9Sstevel@tonic-gate 		!= GSS_S_COMPLETE) {
5637c478bd9Sstevel@tonic-gate 		printf(gettext(
5647c478bd9Sstevel@tonic-gate 			"could not parse desired name: err (octal) %o (%s)\n"),
5657c478bd9Sstevel@tonic-gate 			status, gettext("gss_acquire_cred error"));
5667c478bd9Sstevel@tonic-gate 		return;
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	argc--;
5707c478bd9Sstevel@tonic-gate 	argv++;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * The next argument is an OID in dotted decimal form.
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (argc == 0) {
5777c478bd9Sstevel@tonic-gate 		printf(gettext("Assuming dummy  as the mechanism\n"));
5787c478bd9Sstevel@tonic-gate 		printf(gettext(
5797c478bd9Sstevel@tonic-gate 			"The mech OID 1.3.6.1.4.1.42.2.26.1.2 will be used\n"));
5807c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid((char *)GSS_DUMMY_MECH_OID);
5817c478bd9Sstevel@tonic-gate 	} else
5827c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid(argv[0]);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (mech_type == 0 || mech_type->length == 0) {
5857c478bd9Sstevel@tonic-gate 		printf(gettext("improperly formated mechanism OID\n"));
5867c478bd9Sstevel@tonic-gate 		return;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * set up desired_mechs so it points to mech_type.
5917c478bd9Sstevel@tonic-gate 	 */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	status = kgss_add_cred(
5947c478bd9Sstevel@tonic-gate 				&minor_status,
5957c478bd9Sstevel@tonic-gate 				acceptor_credentials,
5967c478bd9Sstevel@tonic-gate 				desired_name,
5977c478bd9Sstevel@tonic-gate 				mech_type,
5987c478bd9Sstevel@tonic-gate 				cred_usage,
5997c478bd9Sstevel@tonic-gate 				initiator_time_req,
6007c478bd9Sstevel@tonic-gate 				acceptor_time_req,
6017c478bd9Sstevel@tonic-gate 				&actual_mechs,
6027c478bd9Sstevel@tonic-gate 				NULL,
6037c478bd9Sstevel@tonic-gate 				NULL,
6047c478bd9Sstevel@tonic-gate 				uid);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	gss_major_code = status;
6097c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
6107c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
6117c478bd9Sstevel@tonic-gate 		/* process returned values */
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		printf(gettext("\nadd  succeeded\n\n"));
6147c478bd9Sstevel@tonic-gate 		if (actual_mechs) {
6157c478bd9Sstevel@tonic-gate 			for (i = 0; i < actual_mechs->count; i++) {
6167c478bd9Sstevel@tonic-gate 				if ((string =
6177c478bd9Sstevel@tonic-gate 					gss_oid2str
6187c478bd9Sstevel@tonic-gate 					(&actual_mechs->elements[i])) == 0) {
6197c478bd9Sstevel@tonic-gate 					printf(gettext
6207c478bd9Sstevel@tonic-gate 					("actual mechs == NULL\n\n"));
6217c478bd9Sstevel@tonic-gate 				} else {
6227c478bd9Sstevel@tonic-gate 					printf(gettext
6237c478bd9Sstevel@tonic-gate 					("actual mechs  = %s\n\n"), string);
6247c478bd9Sstevel@tonic-gate 					FREE(string,
6257c478bd9Sstevel@tonic-gate 					(actual_mechs->elements->length+1)*4+1);
6267c478bd9Sstevel@tonic-gate 				}
6277c478bd9Sstevel@tonic-gate 			}
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		 * Try adding the cred again for the same mech
6317c478bd9Sstevel@tonic-gate 		 * We should get GSS_S_DUPLICATE_ELEMENT
6327c478bd9Sstevel@tonic-gate 		 * if not return an error
6337c478bd9Sstevel@tonic-gate 		 */
6347c478bd9Sstevel@tonic-gate 		status = kgss_add_cred(
6357c478bd9Sstevel@tonic-gate 				&minor_status,
6367c478bd9Sstevel@tonic-gate 				acceptor_credentials,
6377c478bd9Sstevel@tonic-gate 				desired_name,
6387c478bd9Sstevel@tonic-gate 				mech_type,
6397c478bd9Sstevel@tonic-gate 				cred_usage,
6407c478bd9Sstevel@tonic-gate 				initiator_time_req,
6417c478bd9Sstevel@tonic-gate 				acceptor_time_req,
6427c478bd9Sstevel@tonic-gate 				NULL, /*  &actual_mechs, */
6437c478bd9Sstevel@tonic-gate 				NULL,
6447c478bd9Sstevel@tonic-gate 				NULL,
6457c478bd9Sstevel@tonic-gate 				uid);
6467c478bd9Sstevel@tonic-gate 		if (status != GSS_S_DUPLICATE_ELEMENT) {
6477c478bd9Sstevel@tonic-gate 			printf(gettext("Expected duplicate element, Got "
6487c478bd9Sstevel@tonic-gate 			" (octal) %o (%s)\n"),
6497c478bd9Sstevel@tonic-gate 			status, gettext("gss_add_cred error"));
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		status = kgss_inquire_cred(
6527c478bd9Sstevel@tonic-gate 				&minor_status,
6537c478bd9Sstevel@tonic-gate 				acceptor_credentials,
6547c478bd9Sstevel@tonic-gate 				NULL,
6557c478bd9Sstevel@tonic-gate 				&time_req,
6567c478bd9Sstevel@tonic-gate 				&cred_usage,
6577c478bd9Sstevel@tonic-gate 				&inquire_mechs,
6587c478bd9Sstevel@tonic-gate 				uid);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		if (status != GSS_S_COMPLETE)
6617c478bd9Sstevel@tonic-gate 			printf(gettext("server ret err (octal) %o (%s)\n"),
6627c478bd9Sstevel@tonic-gate 			status, gettext("gss_inquire_cred error"));
6637c478bd9Sstevel@tonic-gate 		else {
6647c478bd9Sstevel@tonic-gate 			for (i = 0; i < inquire_mechs->count; i++) {
6657c478bd9Sstevel@tonic-gate 				if ((string =
6667c478bd9Sstevel@tonic-gate 					gss_oid2str
6677c478bd9Sstevel@tonic-gate 					(&inquire_mechs->elements[i])) == 0) {
6687c478bd9Sstevel@tonic-gate 					printf(gettext
6697c478bd9Sstevel@tonic-gate 					("inquire_mechs mechs == NULL\n\n"));
6707c478bd9Sstevel@tonic-gate 				} else {
6717c478bd9Sstevel@tonic-gate 					printf(gettext
6727c478bd9Sstevel@tonic-gate 					("inquire_cred mechs  = %s\n\n"),
6737c478bd9Sstevel@tonic-gate 						string);
6747c478bd9Sstevel@tonic-gate 					FREE(string,
6757c478bd9Sstevel@tonic-gate 					(inquire_mechs->elements->length+1)*4
6767c478bd9Sstevel@tonic-gate 					+1);
6777c478bd9Sstevel@tonic-gate 				}
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 			printf(gettext("inquire_cred successful \n\n"));
6807c478bd9Sstevel@tonic-gate 		}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	} else {
6837c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
6847c478bd9Sstevel@tonic-gate 			status, gettext("gss_acquire_cred error"));
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* Let us do inquire_cred_by_mech for both mechanisms */
6887c478bd9Sstevel@tonic-gate 	status = kgss_inquire_cred_by_mech(
6897c478bd9Sstevel@tonic-gate 			&minor_status,
6907c478bd9Sstevel@tonic-gate 			acceptor_credentials,
6917c478bd9Sstevel@tonic-gate 			mech_type,
6927c478bd9Sstevel@tonic-gate 			uid);
6937c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE)
6947c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
6957c478bd9Sstevel@tonic-gate 		status, gettext("gss_inquire_cred_by_mech"));
6967c478bd9Sstevel@tonic-gate 	else
6977c478bd9Sstevel@tonic-gate 		printf(gettext("gss_inquire_cred_by_mech successful"));
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	FREE(mech_type->elements, mech_type->length);
7017c478bd9Sstevel@tonic-gate 	FREE(mech_type, sizeof (gss_OID_desc));
7027c478bd9Sstevel@tonic-gate 	mech_type = gss_str2oid((char *)GSS_KRB5_MECH_OID);
7037c478bd9Sstevel@tonic-gate 	status = kgss_inquire_cred_by_mech(
7047c478bd9Sstevel@tonic-gate 			&minor_status,
7057c478bd9Sstevel@tonic-gate 			acceptor_credentials,
7067c478bd9Sstevel@tonic-gate 			mech_type,
7077c478bd9Sstevel@tonic-gate 			uid);
7087c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE)
7097c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
7107c478bd9Sstevel@tonic-gate 			status, gettext
7117c478bd9Sstevel@tonic-gate 			("gss_inquire_cred_by_mech for dummy mech error"));
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/* free allocated memory */
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/* actual mechs is allocated by clnt_stubs. Release it here */
7167c478bd9Sstevel@tonic-gate 	if (actual_mechs != GSS_C_NULL_OID_SET)
7177c478bd9Sstevel@tonic-gate 		gss_release_oid_set_and_oids(&minor_status, &actual_mechs);
7187c478bd9Sstevel@tonic-gate 	if (inquire_mechs != GSS_C_NULL_OID_SET)
7197c478bd9Sstevel@tonic-gate 		gss_release_oid_set_and_oids(&minor_status, &inquire_mechs);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	gss_release_name(&minor_status, &desired_name);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/* mech_type and desired_mechs are allocated above. Release it here */
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	FREE(mech_type->elements, mech_type->length);
7267c478bd9Sstevel@tonic-gate 	FREE(mech_type, sizeof (gss_OID_desc));
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7307c478bd9Sstevel@tonic-gate static void
7317c478bd9Sstevel@tonic-gate _gss_release_cred(argc, argv)
7327c478bd9Sstevel@tonic-gate int argc;
7337c478bd9Sstevel@tonic-gate char **argv;
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
7367c478bd9Sstevel@tonic-gate 	OM_UINT32 minor_status;
7377c478bd9Sstevel@tonic-gate 	uid_t uid;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	/* set up input arguments here */
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (argc != 0) {
7427c478bd9Sstevel@tonic-gate 		usage();
7437c478bd9Sstevel@tonic-gate 		return;
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	uid = getuid();
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	status = kgss_release_cred(
7497c478bd9Sstevel@tonic-gate 				&minor_status,
7507c478bd9Sstevel@tonic-gate 				&acceptor_credentials,
7517c478bd9Sstevel@tonic-gate 				uid);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	gss_major_code = status;
7567c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
7597c478bd9Sstevel@tonic-gate 		printf(gettext("\nrelease succeeded\n\n"));
7607c478bd9Sstevel@tonic-gate 	} else {
7617c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
7627c478bd9Sstevel@tonic-gate 			status, gettext("gss_release_cred error"));
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate static void
7677c478bd9Sstevel@tonic-gate _gss_init_sec_context(argc, argv)
7687c478bd9Sstevel@tonic-gate int argc;
7697c478bd9Sstevel@tonic-gate char **argv;
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	OM_uint32 status;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
7757c478bd9Sstevel@tonic-gate 	gss_cred_id_t claimant_cred_handle;
7767c478bd9Sstevel@tonic-gate 	gss_name_t target_name = (gss_name_t) 0;
7777c478bd9Sstevel@tonic-gate 	gss_OID mech_type = (gss_OID) 0;
7787c478bd9Sstevel@tonic-gate 	int req_flags;
7797c478bd9Sstevel@tonic-gate 	OM_uint32 time_req;
7807c478bd9Sstevel@tonic-gate 	gss_channel_bindings_t input_chan_bindings;
7817c478bd9Sstevel@tonic-gate 	gss_buffer_t input_token;
7827c478bd9Sstevel@tonic-gate 	gss_buffer_desc context_token;
7837c478bd9Sstevel@tonic-gate 	gss_OID actual_mech_type;
7847c478bd9Sstevel@tonic-gate 	int ret_flags;
7857c478bd9Sstevel@tonic-gate 	OM_uint32 time_rec;
7867c478bd9Sstevel@tonic-gate 	uid_t uid;
7877c478bd9Sstevel@tonic-gate 	char * string;
7887c478bd9Sstevel@tonic-gate 	gss_buffer_desc name;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/*
7917c478bd9Sstevel@tonic-gate 	 * If this is the first phase of the context establishment,
7927c478bd9Sstevel@tonic-gate 	 * clear initiator_context_handle and indicate next phase.
7937c478bd9Sstevel@tonic-gate 	 */
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (init_sec_context_phase == 0) {
7967c478bd9Sstevel@tonic-gate 		initiator_context_handle = GSS_C_NO_CONTEXT;
7977c478bd9Sstevel@tonic-gate 		input_token = GSS_C_NO_BUFFER;
7987c478bd9Sstevel@tonic-gate 		init_sec_context_phase = 1;
7997c478bd9Sstevel@tonic-gate 	} else
8007c478bd9Sstevel@tonic-gate 		input_token = &init_token_buffer;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	/*
8037c478bd9Sstevel@tonic-gate 	 * First set up the non-variable command line independent input
8047c478bd9Sstevel@tonic-gate 	 * arguments
8057c478bd9Sstevel@tonic-gate 	 */
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	claimant_cred_handle = GSS_C_NO_CREDENTIAL;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	req_flags = GSS_C_MUTUAL_FLAG;
8107c478bd9Sstevel@tonic-gate 	time_req = (OM_uint32) 0;
8117c478bd9Sstevel@tonic-gate 	input_chan_bindings = GSS_C_NO_CHANNEL_BINDINGS;
8127c478bd9Sstevel@tonic-gate 	uid = getuid();
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/* Now parse the command line for the remaining input arguments */
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	if (argc == 0) {
8177c478bd9Sstevel@tonic-gate 		usage();
8187c478bd9Sstevel@tonic-gate 		return;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/*
8227c478bd9Sstevel@tonic-gate 	 * Get the name of the target.
8237c478bd9Sstevel@tonic-gate 	 */
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	name.length = strlen(argv[0])+1;
8267c478bd9Sstevel@tonic-gate 	name.value = argv[0];
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/*
8297c478bd9Sstevel@tonic-gate 	 * Now convert the string given by the first argument into a target
8307c478bd9Sstevel@tonic-gate 	 * name suitable for input to gss_init_sec_context()
8317c478bd9Sstevel@tonic-gate 	 */
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if ((status = gss_import_name(&minor_status, &name,
8347c478bd9Sstevel@tonic-gate 		/* GSS_C_NULL_OID, &target_name)) */
8357c478bd9Sstevel@tonic-gate 		(gss_OID)GSS_C_NT_HOSTBASED_SERVICE, &target_name))
8367c478bd9Sstevel@tonic-gate 		!= GSS_S_COMPLETE) {
8377c478bd9Sstevel@tonic-gate 		printf(gettext(
8387c478bd9Sstevel@tonic-gate 			"could not parse target name: err (octal) %o (%s)\n"),
8397c478bd9Sstevel@tonic-gate 			status,
8407c478bd9Sstevel@tonic-gate 			gettext("gss_init_sec_context error"));
8417c478bd9Sstevel@tonic-gate 		if (input_token != GSS_C_NO_BUFFER)
8427c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &init_token_buffer);
8437c478bd9Sstevel@tonic-gate 		init_sec_context_phase = 0;
8447c478bd9Sstevel@tonic-gate 		return;
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	argc--;
8487c478bd9Sstevel@tonic-gate 	argv++;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (argc == 0) {
8517c478bd9Sstevel@tonic-gate 		printf(gettext("Assuming Kerberos V5 as the mechanism\n"));
8527c478bd9Sstevel@tonic-gate 		printf(gettext(
8537c478bd9Sstevel@tonic-gate 			"The mech OID 1.2.840.113554.1.2.2 will be used\n"));
8547c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid((char *)GSS_KRB5_MECH_OID);
8557c478bd9Sstevel@tonic-gate 	} else {
8567c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid(argv[0]);
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if (mech_type == 0 || mech_type->length == 0) {
8607c478bd9Sstevel@tonic-gate 		printf(gettext("improperly formated mechanism OID\n"));
8617c478bd9Sstevel@tonic-gate 		if (input_token != GSS_C_NO_BUFFER)
8627c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &init_token_buffer);
8637c478bd9Sstevel@tonic-gate 		init_sec_context_phase = 0;
8647c478bd9Sstevel@tonic-gate 		return;
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/* call kgss_init_sec_context */
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	status = kgss_init_sec_context(&minor_status,
8707c478bd9Sstevel@tonic-gate 				claimant_cred_handle,
8717c478bd9Sstevel@tonic-gate 				&initiator_context_handle,
8727c478bd9Sstevel@tonic-gate 				target_name,
8737c478bd9Sstevel@tonic-gate 				mech_type,
8747c478bd9Sstevel@tonic-gate 				req_flags,
8757c478bd9Sstevel@tonic-gate 				time_req,
8767c478bd9Sstevel@tonic-gate 				input_chan_bindings,
8777c478bd9Sstevel@tonic-gate 				input_token,
8787c478bd9Sstevel@tonic-gate 				&actual_mech_type,
8797c478bd9Sstevel@tonic-gate 				&accept_token_buffer,
8807c478bd9Sstevel@tonic-gate 				&ret_flags,
8817c478bd9Sstevel@tonic-gate 				&time_rec,
8827c478bd9Sstevel@tonic-gate 				uid);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
8857c478bd9Sstevel@tonic-gate 	gss_major_code = status;
8867c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE &&
8897c478bd9Sstevel@tonic-gate 	    status != GSS_S_CONTINUE_NEEDED) {
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
8927c478bd9Sstevel@tonic-gate 			status, "gss_init_sec_context error");
8937c478bd9Sstevel@tonic-gate 		init_sec_context_phase = 0;
8947c478bd9Sstevel@tonic-gate 		if (status == GSS_S_NO_CRED)
8957c478bd9Sstevel@tonic-gate 			printf(gettext(" : no credentials"));
8967c478bd9Sstevel@tonic-gate 		if (input_token != GSS_C_NO_BUFFER)
8977c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &init_token_buffer);
8987c478bd9Sstevel@tonic-gate 		if (status != GSS_S_FAILURE && minor_status != 0xffffffff)
8997c478bd9Sstevel@tonic-gate 			status = kgss_delete_sec_context(&minor_status,
9007c478bd9Sstevel@tonic-gate 					&initiator_context_handle,
9017c478bd9Sstevel@tonic-gate 					&msg_token);
9027c478bd9Sstevel@tonic-gate 		return;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	} else if (status == GSS_S_COMPLETE) {
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		/* process returned values */
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		printf(gettext("\ninit succeeded\n\n"));
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		/* print out the actual mechanism type */
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		if ((string = gss_oid2str(actual_mech_type)) == 0) {
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 			printf(gettext(
9157c478bd9Sstevel@tonic-gate 				"gssapi internal err : actual "
9167c478bd9Sstevel@tonic-gate 				"mech type null\n"));
9177c478bd9Sstevel@tonic-gate 			init_sec_context_phase = 0;
9187c478bd9Sstevel@tonic-gate 			if (input_token != GSS_C_NO_BUFFER)
9197c478bd9Sstevel@tonic-gate 				gss_release_buffer(&minor_status,
9207c478bd9Sstevel@tonic-gate 						&init_token_buffer);
9217c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &accept_token_buffer);
9227c478bd9Sstevel@tonic-gate 			status = kgss_delete_sec_context(&minor_status,
9237c478bd9Sstevel@tonic-gate 					&initiator_context_handle,
9247c478bd9Sstevel@tonic-gate 					&msg_token);
9257c478bd9Sstevel@tonic-gate 			return;
9267c478bd9Sstevel@tonic-gate 		} else {
9277c478bd9Sstevel@tonic-gate 			printf(gettext("actual mech type = %s\n\n"), string);
9287c478bd9Sstevel@tonic-gate 			FREE(string, (actual_mech_type->length+1)*4+1);
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		/* print out value of ret_flags and time_req */
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_DELEG_FLAG)
9347c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_DELEG_FLAG = True\n"));
9357c478bd9Sstevel@tonic-gate 		else
9367c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_DELEG_FLAG = False\n"));
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_MUTUAL_FLAG)
9397c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_MUTUAL_FLAG = True\n"));
9407c478bd9Sstevel@tonic-gate 		else
9417c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_MUTUAL_FLAG = False\n"));
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_REPLAY_FLAG)
9447c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_REPLAY_FLAG = True\n"));
9457c478bd9Sstevel@tonic-gate 		else
9467c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_REPLAY_FLAG = False\n"));
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_SEQUENCE_FLAG)
9497c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_SEQUENCE_FLAG = True\n"));
9507c478bd9Sstevel@tonic-gate 		else
9517c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_SEQUENCE_FLAG = False\n"));
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_CONF_FLAG)
9547c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_CONF_FLAG = True\n"));
9557c478bd9Sstevel@tonic-gate 		else
9567c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_CONF_FLAG = False\n"));
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_INTEG_FLAG)
9597c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_INTEG_FLAG = True\n\n"));
9607c478bd9Sstevel@tonic-gate 		else
9617c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_INTEG_FLAG = False\n\n"));
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 		printf(gettext("time_req = %u seconds\n\n"), time_rec);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 		/* free allocated memory */
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		FREE(mech_type->elements, mech_type->length);
9687c478bd9Sstevel@tonic-gate 		FREE(mech_type, sizeof (gss_OID_desc));
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		/* these two were malloc'd by kgss_init_sec_context() */
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		FREE(actual_mech_type->elements, actual_mech_type->length);
9737c478bd9Sstevel@tonic-gate 		FREE(actual_mech_type, sizeof (gss_OID_desc));
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		gss_release_name(&minor_status, &target_name);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		if (input_token != GSS_C_NO_BUFFER)
9787c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &init_token_buffer);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		/*
9817c478bd9Sstevel@tonic-gate 		 * if status == GSS_S_COMPLETE, reset the phase to 0 and
9827c478bd9Sstevel@tonic-gate 		 * release token in accept_token_buffer
9837c478bd9Sstevel@tonic-gate 		 */
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		init_sec_context_phase = 0;
9867c478bd9Sstevel@tonic-gate 	/* Save and restore the context */
9877c478bd9Sstevel@tonic-gate 	status = kgss_export_sec_context(&minor_status,
9887c478bd9Sstevel@tonic-gate 					&initiator_context_handle,
9897c478bd9Sstevel@tonic-gate 					&context_token);
9907c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
9917c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
9927c478bd9Sstevel@tonic-gate 			status, gettext("gss_export_sec_context_error"));
9937c478bd9Sstevel@tonic-gate 		return;
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 	status = kgss_import_sec_context(&minor_status,
9967c478bd9Sstevel@tonic-gate 					&context_token,
9977c478bd9Sstevel@tonic-gate 					&initiator_context_handle);
9987c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
9997c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
10007c478bd9Sstevel@tonic-gate 			status, gettext("gss_import_sec_context_error"));
10017c478bd9Sstevel@tonic-gate 		return;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&minor_status, &context_token);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/* gss_export & gss_import secxc_context worked, return */
10067c478bd9Sstevel@tonic-gate 	printf(gettext("\nexport and import of contexts succeeded\n"));
10077c478bd9Sstevel@tonic-gate 	printf(gettext("\ninit completed"));
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	} else {
10107c478bd9Sstevel@tonic-gate 		printf(gettext("\nfirst phase of init succeeded"));
10117c478bd9Sstevel@tonic-gate 		printf(gettext("\ninit must be called again\n\n"));
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10177c478bd9Sstevel@tonic-gate static void
10187c478bd9Sstevel@tonic-gate _gss_accept_sec_context(argc, argv)
10197c478bd9Sstevel@tonic-gate int argc;
10207c478bd9Sstevel@tonic-gate char **argv;
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
10257c478bd9Sstevel@tonic-gate 	gss_channel_bindings_t input_chan_bindings;
10267c478bd9Sstevel@tonic-gate 	gss_OID mech_type;
10277c478bd9Sstevel@tonic-gate 	int ret_flags;
10287c478bd9Sstevel@tonic-gate 	OM_uint32 time_rec;
10297c478bd9Sstevel@tonic-gate 	gss_cred_id_t delegated_cred_handle;
10307c478bd9Sstevel@tonic-gate 	uid_t uid;
10317c478bd9Sstevel@tonic-gate 	char *string;
10327c478bd9Sstevel@tonic-gate 	gss_buffer_desc src_name, src_name_string;
10337c478bd9Sstevel@tonic-gate 	gss_buffer_desc output_token;
10347c478bd9Sstevel@tonic-gate 	gss_name_t gss_name;
10357c478bd9Sstevel@tonic-gate 	gss_buffer_desc context_token;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	/*
10387c478bd9Sstevel@tonic-gate 	 * If this is the first phase of the context establishment,
10397c478bd9Sstevel@tonic-gate 	 * clear acceptor_context_handle and indicate next phase.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	if (accept_sec_context_phase == 0) {
10437c478bd9Sstevel@tonic-gate 		acceptor_context_handle = GSS_C_NO_CONTEXT;
10447c478bd9Sstevel@tonic-gate 		accept_sec_context_phase = 1;
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/* Now set up the other command line independent input arguments */
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	input_chan_bindings = GSS_C_NO_CHANNEL_BINDINGS;
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if (argc != 0) {
10547c478bd9Sstevel@tonic-gate 		usage();
10557c478bd9Sstevel@tonic-gate 		return;
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	status = kgss_accept_sec_context(&minor_status,
10597c478bd9Sstevel@tonic-gate 					&acceptor_context_handle,
10607c478bd9Sstevel@tonic-gate 					acceptor_credentials,
10617c478bd9Sstevel@tonic-gate 					&accept_token_buffer,
10627c478bd9Sstevel@tonic-gate 					input_chan_bindings,
10637c478bd9Sstevel@tonic-gate 					&src_name,
10647c478bd9Sstevel@tonic-gate 					&mech_type,
10657c478bd9Sstevel@tonic-gate 					&init_token_buffer,
10667c478bd9Sstevel@tonic-gate 					&ret_flags,
10677c478bd9Sstevel@tonic-gate 					&time_rec,
10687c478bd9Sstevel@tonic-gate 					&delegated_cred_handle,
10697c478bd9Sstevel@tonic-gate 					uid);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	gss_major_code = status;
10747c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE && status != GSS_S_CONTINUE_NEEDED) {
10777c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
10787c478bd9Sstevel@tonic-gate 			status, gettext("gss_accept_sec_context error"));
10797c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor_status, &accept_token_buffer);
10807c478bd9Sstevel@tonic-gate 		return;
10817c478bd9Sstevel@tonic-gate 	} else if (status == GSS_S_COMPLETE) {
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 		/* process returned values */
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 		printf(gettext("\naccept succeeded\n\n"));
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 		/*
10887c478bd9Sstevel@tonic-gate 		 * convert the exported name returned in src_name into
10897c478bd9Sstevel@tonic-gate 		 * a string and print it.
10907c478bd9Sstevel@tonic-gate 		 */
10917c478bd9Sstevel@tonic-gate 		if ((status = gss_import_name(&minor_status, &src_name,
10927c478bd9Sstevel@tonic-gate 			(gss_OID) GSS_C_NT_EXPORT_NAME, &gss_name))
10937c478bd9Sstevel@tonic-gate 			!= GSS_S_COMPLETE) {
10947c478bd9Sstevel@tonic-gate 			printf(gettext(
10957c478bd9Sstevel@tonic-gate 				"could not import src name 0x%x\n"), status);
10967c478bd9Sstevel@tonic-gate 			accept_sec_context_phase = 0;
10977c478bd9Sstevel@tonic-gate 			status = kgss_delete_sec_context(&minor_status,
10987c478bd9Sstevel@tonic-gate 					&acceptor_context_handle,
10997c478bd9Sstevel@tonic-gate 					&output_token);
11007c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &accept_token_buffer);
11017c478bd9Sstevel@tonic-gate 			if (status == GSS_S_CONTINUE_NEEDED)
11027c478bd9Sstevel@tonic-gate 				gss_release_buffer(&minor_status,
11037c478bd9Sstevel@tonic-gate 						&init_token_buffer);
11047c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &src_name);
11057c478bd9Sstevel@tonic-gate 			return;
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		memset(&src_name_string, 0, sizeof (src_name_string));
11097c478bd9Sstevel@tonic-gate 		if ((status = gss_display_name(&minor_status, gss_name,
11107c478bd9Sstevel@tonic-gate 			&src_name_string, NULL)) != GSS_S_COMPLETE) {
11117c478bd9Sstevel@tonic-gate 			printf(gettext("could not display src name: "
11127c478bd9Sstevel@tonic-gate 				"err (octal) %o (%s)\n"), status,
11137c478bd9Sstevel@tonic-gate 				"gss_init_sec_context error");
11147c478bd9Sstevel@tonic-gate 			accept_sec_context_phase = 0;
11157c478bd9Sstevel@tonic-gate 			status = kgss_delete_sec_context(&minor_status,
11167c478bd9Sstevel@tonic-gate 					&acceptor_context_handle,
11177c478bd9Sstevel@tonic-gate 					&output_token);
11187c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &accept_token_buffer);
11197c478bd9Sstevel@tonic-gate 			if (status == GSS_S_CONTINUE_NEEDED)
11207c478bd9Sstevel@tonic-gate 				gss_release_buffer(&minor_status,
11217c478bd9Sstevel@tonic-gate 						&init_token_buffer);
11227c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &src_name);
11237c478bd9Sstevel@tonic-gate 			return;
11247c478bd9Sstevel@tonic-gate 		}
11257c478bd9Sstevel@tonic-gate 		printf(gettext("src name = %s\n"), src_name_string.value);
11267c478bd9Sstevel@tonic-gate 		gss_release_name(&minor_status, &gss_name);
11277c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor_status, &src_name_string);
11287c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor_status, &src_name);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 		/* print out the mechanism type */
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 		if ((string = gss_oid2str(mech_type)) == 0) {
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 			printf(gettext(
11357c478bd9Sstevel@tonic-gate 				"gssapi internal err :"
11367c478bd9Sstevel@tonic-gate 				" actual mech type null\n"));
11377c478bd9Sstevel@tonic-gate 			accept_sec_context_phase = 0;
11387c478bd9Sstevel@tonic-gate 			status = kgss_delete_sec_context(&minor_status,
11397c478bd9Sstevel@tonic-gate 					&acceptor_context_handle,
11407c478bd9Sstevel@tonic-gate 					&output_token);
11417c478bd9Sstevel@tonic-gate 			gss_release_buffer(&minor_status, &accept_token_buffer);
11427c478bd9Sstevel@tonic-gate 			if (status == GSS_S_CONTINUE_NEEDED)
11437c478bd9Sstevel@tonic-gate 				gss_release_buffer(&minor_status,
11447c478bd9Sstevel@tonic-gate 						&init_token_buffer);
11457c478bd9Sstevel@tonic-gate 			return;
11467c478bd9Sstevel@tonic-gate 		} else {
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 			printf(gettext("actual mech type = %s\n\n"), string);
11497c478bd9Sstevel@tonic-gate 			FREE(string, (mech_type->length+1)*4+1);
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/* Save and restore the context */
11537c478bd9Sstevel@tonic-gate 	status = kgss_export_sec_context(&minor_status,
11547c478bd9Sstevel@tonic-gate 					&initiator_context_handle,
11557c478bd9Sstevel@tonic-gate 					&context_token);
11567c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
11577c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
11587c478bd9Sstevel@tonic-gate 			status, gettext("gss_export_sec_context_error"));
11597c478bd9Sstevel@tonic-gate 		return;
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 	status = kgss_import_sec_context(&minor_status,
11627c478bd9Sstevel@tonic-gate 					&context_token,
11637c478bd9Sstevel@tonic-gate 					&initiator_context_handle);
11647c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
11657c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
11667c478bd9Sstevel@tonic-gate 			status, gettext("gss_import_sec_context_error"));
11677c478bd9Sstevel@tonic-gate 		return;
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&minor_status, &context_token);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	/* gss_export & gss_import secxc_context worked, return */
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	/* print out value of ret_flags and time_req */
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_DELEG_FLAG)
11767c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_DELEG_FLAG = True\n"));
11777c478bd9Sstevel@tonic-gate 		else
11787c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_DELEG_FLAG = False\n"));
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_MUTUAL_FLAG)
11817c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_MUTUAL_FLAG = True\n"));
11827c478bd9Sstevel@tonic-gate 		else
11837c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_MUTUAL_FLAG = False\n"));
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_REPLAY_FLAG)
11867c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_REPLAY_FLAG = True\n"));
11877c478bd9Sstevel@tonic-gate 		else
11887c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_REPLAY_FLAG = False\n"));
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_SEQUENCE_FLAG)
11917c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_SEQUENCE_FLAG = True\n"));
11927c478bd9Sstevel@tonic-gate 		else
11937c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_SEQUENCE_FLAG = False\n"));
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_CONF_FLAG)
11967c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_CONF_FLAG = True\n"));
11977c478bd9Sstevel@tonic-gate 		else
11987c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_CONF_FLAG = False\n"));
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 		if (ret_flags & GSS_C_INTEG_FLAG)
12017c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_INTEG_FLAG = True\n\n"));
12027c478bd9Sstevel@tonic-gate 		else
12037c478bd9Sstevel@tonic-gate 			printf(gettext("GSS_C_INTEG_FLAG = False\n\n"));
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		printf(gettext("time_rec = %d seconds\n\n"), time_rec);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		/* free allocated memory */
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		printf(gettext("\nexport and import of contexts succeeded\n"));
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 		FREE(mech_type->elements, mech_type->length);
12127c478bd9Sstevel@tonic-gate 		FREE(mech_type, sizeof (gss_OID_desc));
12137c478bd9Sstevel@tonic-gate 	} else {
12147c478bd9Sstevel@tonic-gate 		printf(gettext("\nfirst phase of accept succeeded"));
12157c478bd9Sstevel@tonic-gate 		printf(gettext("\naccept must be called again\n\n"));
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	/* free the input token in accept_token_buffer */
12207c478bd9Sstevel@tonic-gate 	gss_release_buffer(&minor_status, &accept_token_buffer);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/* if status == GSS_S_COMPLETE, reset the phase to 0 */
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE)
12257c478bd9Sstevel@tonic-gate 		accept_sec_context_phase = 0;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/* gss_accept_sec_context worked, return */
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate void
12317c478bd9Sstevel@tonic-gate _gss_process_context_token(argc, argv)
12327c478bd9Sstevel@tonic-gate int argc;
12337c478bd9Sstevel@tonic-gate char **argv;
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	gss_ctx_id_t context_handle;
12387c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
12397c478bd9Sstevel@tonic-gate 	uid_t uid;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/* parse the command line to determine the variable input argument */
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	if (argc == 0) {
12467c478bd9Sstevel@tonic-gate 		usage();
12477c478bd9Sstevel@tonic-gate 		return;
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "initiator") == 0)
12517c478bd9Sstevel@tonic-gate 		context_handle = initiator_context_handle;
12527c478bd9Sstevel@tonic-gate 	else if (strcmp(argv[0], "acceptor") == 0)
12537c478bd9Sstevel@tonic-gate 		context_handle = acceptor_context_handle;
12547c478bd9Sstevel@tonic-gate 	else {
12557c478bd9Sstevel@tonic-gate 		printf(gettext(
12567c478bd9Sstevel@tonic-gate 			"must specify either \"initiator\" or \"acceptor\"\n"));
12577c478bd9Sstevel@tonic-gate 		return;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	argc--;
12617c478bd9Sstevel@tonic-gate 	argv++;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	if (argc != 0) {
12647c478bd9Sstevel@tonic-gate 		usage();
12657c478bd9Sstevel@tonic-gate 		return;
12667c478bd9Sstevel@tonic-gate 	}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	status = kgss_process_context_token(&minor_status,
12697c478bd9Sstevel@tonic-gate 					    context_handle,
12707c478bd9Sstevel@tonic-gate 					    delete_token_buffer,
12717c478bd9Sstevel@tonic-gate 					    uid);
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	gss_major_code = status;
12767c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
12797c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
12807c478bd9Sstevel@tonic-gate 			status, gettext("gss_process_context_token error"));
12817c478bd9Sstevel@tonic-gate 		return;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	} else {
12847c478bd9Sstevel@tonic-gate 		printf(gettext("\nprocess succeeded\n\n"));
12857c478bd9Sstevel@tonic-gate 		return;
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate static void
12907c478bd9Sstevel@tonic-gate _gss_delete_sec_context(argc, argv)
12917c478bd9Sstevel@tonic-gate int argc;
12927c478bd9Sstevel@tonic-gate char **argv;
12937c478bd9Sstevel@tonic-gate {
12947c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
12957c478bd9Sstevel@tonic-gate 	gss_ctx_id_t *context_handle;
12967c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
12977c478bd9Sstevel@tonic-gate 	uid_t uid;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	/* parse the command line to determine the variable input argument */
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	if (argc == 0) {
13047c478bd9Sstevel@tonic-gate 		usage();
13057c478bd9Sstevel@tonic-gate 		return;
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "initiator") == 0) {
13097c478bd9Sstevel@tonic-gate 		context_handle = &initiator_context_handle;
13107c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[0], "acceptor") == 0) {
13117c478bd9Sstevel@tonic-gate 		context_handle = &acceptor_context_handle;
13127c478bd9Sstevel@tonic-gate 	} else {
13137c478bd9Sstevel@tonic-gate 		printf(gettext(
13147c478bd9Sstevel@tonic-gate 			"must specify either \"initiator\" or \"acceptor\"\n"));
13157c478bd9Sstevel@tonic-gate 		return;
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	argc--;
13197c478bd9Sstevel@tonic-gate 	argv++;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (argc != 0) {
13227c478bd9Sstevel@tonic-gate 		usage();
13237c478bd9Sstevel@tonic-gate 		return;
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	status = kgss_delete_sec_context(&minor_status,
13287c478bd9Sstevel@tonic-gate 					context_handle,
13297c478bd9Sstevel@tonic-gate 					&delete_token_buffer);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	gss_major_code = status;
13357c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
13407c478bd9Sstevel@tonic-gate 			status, gettext("gss_delete_sec_context error"));
13417c478bd9Sstevel@tonic-gate 		return;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	} else {
13447c478bd9Sstevel@tonic-gate 		printf(gettext("\ndelete succeeded\n\n"));
13457c478bd9Sstevel@tonic-gate 		return;
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13507c478bd9Sstevel@tonic-gate static void
13517c478bd9Sstevel@tonic-gate _gss_context_time(argc, argv)
13527c478bd9Sstevel@tonic-gate int argc;
13537c478bd9Sstevel@tonic-gate char **argv;
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * set up input arguments here
13577c478bd9Sstevel@tonic-gate 	 * this function is unimplemented. Call usage() and return
13587c478bd9Sstevel@tonic-gate 	 */
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	printf(gettext("\nunimplemented function"));
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate static void
13647c478bd9Sstevel@tonic-gate _gss_sign(argc, argv)
13657c478bd9Sstevel@tonic-gate int argc;
13667c478bd9Sstevel@tonic-gate char **argv;
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
13697c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
13707c478bd9Sstevel@tonic-gate 	gss_ctx_id_t context_handle;
13717c478bd9Sstevel@tonic-gate 	int qop_req;
13727c478bd9Sstevel@tonic-gate 	uid_t uid;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/* specify the default quality of protection */
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	qop_req = GSS_C_QOP_DEFAULT;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	/* set up the arguments specified in the input parameters */
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	if (argc == 0) {
13837c478bd9Sstevel@tonic-gate 		usage();
13847c478bd9Sstevel@tonic-gate 		return;
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "initiator") == 0)
13897c478bd9Sstevel@tonic-gate 		context_handle = initiator_context_handle;
13907c478bd9Sstevel@tonic-gate 	else if (strcmp(argv[0], "acceptor") == 0)
13917c478bd9Sstevel@tonic-gate 		context_handle = acceptor_context_handle;
13927c478bd9Sstevel@tonic-gate 	else {
13937c478bd9Sstevel@tonic-gate 		printf(gettext(
13947c478bd9Sstevel@tonic-gate 			"must specify either \"initiator\" or \"acceptor\"\n"));
13957c478bd9Sstevel@tonic-gate 		return;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	argc--;
13997c478bd9Sstevel@tonic-gate 	argv++;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (argc == 0) {
14027c478bd9Sstevel@tonic-gate 		usage();
14037c478bd9Sstevel@tonic-gate 		return;
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	message_buffer.length = strlen(argv[0])+1;
14077c478bd9Sstevel@tonic-gate 	message_buffer.value = (void *) MALLOC(message_buffer.length);
14087c478bd9Sstevel@tonic-gate 	strcpy(message_buffer.value, argv[0]);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	argc--;
14117c478bd9Sstevel@tonic-gate 	argv++;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	if (argc != 0) {
14147c478bd9Sstevel@tonic-gate 		usage();
14157c478bd9Sstevel@tonic-gate 		return;
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	status = kgss_sign(&minor_status,
14197c478bd9Sstevel@tonic-gate 			context_handle,
14207c478bd9Sstevel@tonic-gate 			qop_req,
14217c478bd9Sstevel@tonic-gate 			&message_buffer,
14227c478bd9Sstevel@tonic-gate 			&msg_token,
14237c478bd9Sstevel@tonic-gate 			uid);
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	gss_major_code = status;
14287c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
14317c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
14327c478bd9Sstevel@tonic-gate 			status, gettext("gss_sign error"));
14337c478bd9Sstevel@tonic-gate 		return;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	} else {
14367c478bd9Sstevel@tonic-gate 		printf(gettext("\nsign succeeded\n\n"));
14377c478bd9Sstevel@tonic-gate 		return;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate static void
14427c478bd9Sstevel@tonic-gate _gss_verify(argc, argv)
14437c478bd9Sstevel@tonic-gate int argc;
14447c478bd9Sstevel@tonic-gate char **argv;
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	OM_UINT32 status, minor_status;
14477c478bd9Sstevel@tonic-gate 	gss_ctx_id_t context_handle;
14487c478bd9Sstevel@tonic-gate 	int qop_state;
14497c478bd9Sstevel@tonic-gate 	uid_t uid;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/* set up the arguments specified in the input parameters */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if (argc == 0) {
14567c478bd9Sstevel@tonic-gate 		usage();
14577c478bd9Sstevel@tonic-gate 		return;
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "initiator") == 0)
14627c478bd9Sstevel@tonic-gate 		context_handle = initiator_context_handle;
14637c478bd9Sstevel@tonic-gate 	else if (strcmp(argv[0], "acceptor") == 0)
14647c478bd9Sstevel@tonic-gate 		context_handle = acceptor_context_handle;
14657c478bd9Sstevel@tonic-gate 	else {
14667c478bd9Sstevel@tonic-gate 		printf(gettext(
14677c478bd9Sstevel@tonic-gate 			"must specify either \"initiator\" or \"acceptor\"\n"));
14687c478bd9Sstevel@tonic-gate 		return;
14697c478bd9Sstevel@tonic-gate 	}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	argc--;
14727c478bd9Sstevel@tonic-gate 	argv++;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if (argc != 0) {
14757c478bd9Sstevel@tonic-gate 		usage();
14767c478bd9Sstevel@tonic-gate 		return;
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	status = kgss_verify(&minor_status,
14807c478bd9Sstevel@tonic-gate 			context_handle,
14817c478bd9Sstevel@tonic-gate 			&message_buffer,
14827c478bd9Sstevel@tonic-gate 			&msg_token,
14837c478bd9Sstevel@tonic-gate 			&qop_state,
14847c478bd9Sstevel@tonic-gate 			uid);
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	gss_major_code = status;
14897c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
14927c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
14937c478bd9Sstevel@tonic-gate 			status, gettext("gss_verify error"));
14947c478bd9Sstevel@tonic-gate 		return;
14957c478bd9Sstevel@tonic-gate 	} else {
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 		/* print out the verified message */
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 		printf(gettext(
15007c478bd9Sstevel@tonic-gate 			"verified message = \"%s\"\n\n"), message_buffer.value);
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 		/* print out the quality of protection returned */
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		printf(gettext("quality of protection = %d \n\n"), qop_state);
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 		/* free the message buffer and message token and return */
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor_status, &message_buffer);
15097c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor_status, &msg_token);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		return;
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate static void
15167c478bd9Sstevel@tonic-gate _gss_seal(argc, argv)
15177c478bd9Sstevel@tonic-gate int argc;
15187c478bd9Sstevel@tonic-gate char **argv;
15197c478bd9Sstevel@tonic-gate {
15207c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
15237c478bd9Sstevel@tonic-gate 	gss_ctx_id_t context_handle;
15247c478bd9Sstevel@tonic-gate 	int conf_req_flag;
15257c478bd9Sstevel@tonic-gate 	int qop_req;
15267c478bd9Sstevel@tonic-gate 	gss_buffer_desc input_message_buffer;
15277c478bd9Sstevel@tonic-gate 	int conf_state;
15287c478bd9Sstevel@tonic-gate 	uid_t uid;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	/*
15337c478bd9Sstevel@tonic-gate 	 * specify the default confidentiality requested (both integrity
15347c478bd9Sstevel@tonic-gate 	 * and confidentiality) and quality of protection
15357c478bd9Sstevel@tonic-gate 	 */
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	conf_req_flag = 1;
15387c478bd9Sstevel@tonic-gate 	qop_req = GSS_C_QOP_DEFAULT;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	/* set up the arguments specified in the input parameters */
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	if (argc == 0) {
15437c478bd9Sstevel@tonic-gate 		usage();
15447c478bd9Sstevel@tonic-gate 		return;
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "initiator") == 0)
15497c478bd9Sstevel@tonic-gate 		context_handle = initiator_context_handle;
15507c478bd9Sstevel@tonic-gate 	else if (strcmp(argv[0], "acceptor") == 0)
15517c478bd9Sstevel@tonic-gate 		context_handle = acceptor_context_handle;
15527c478bd9Sstevel@tonic-gate 	else {
15537c478bd9Sstevel@tonic-gate 		printf(gettext(
15547c478bd9Sstevel@tonic-gate 			"must specify either \"initiator\" or \"acceptor\"\n"));
15557c478bd9Sstevel@tonic-gate 		return;
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	argc--;
15597c478bd9Sstevel@tonic-gate 	argv++;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	if (argc == 0) {
15627c478bd9Sstevel@tonic-gate 		usage();
15637c478bd9Sstevel@tonic-gate 		return;
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	input_message_buffer.length = strlen(argv[0])+1;
15687c478bd9Sstevel@tonic-gate 	input_message_buffer.value =
15697c478bd9Sstevel@tonic-gate 		(void *) MALLOC(input_message_buffer.length);
15707c478bd9Sstevel@tonic-gate 	strcpy(input_message_buffer.value, argv[0]);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	argc--;
15737c478bd9Sstevel@tonic-gate 	argv++;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	if (argc != 0) {
15767c478bd9Sstevel@tonic-gate 		usage();
15777c478bd9Sstevel@tonic-gate 		return;
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	status = kgss_seal(&minor_status,
15817c478bd9Sstevel@tonic-gate 			context_handle,
15827c478bd9Sstevel@tonic-gate 			conf_req_flag,
15837c478bd9Sstevel@tonic-gate 			qop_req,
15847c478bd9Sstevel@tonic-gate 			&input_message_buffer,
15857c478bd9Sstevel@tonic-gate 			&conf_state,
15867c478bd9Sstevel@tonic-gate 			&message_buffer,
15877c478bd9Sstevel@tonic-gate 			uid);
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	gss_major_code = status;
15927c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/* free the inputmessage buffer */
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	gss_release_buffer(&minor_status, &input_message_buffer);
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
15997c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
16007c478bd9Sstevel@tonic-gate 			status, gettext("gss_seal error"));
16017c478bd9Sstevel@tonic-gate 		return;
16027c478bd9Sstevel@tonic-gate 	} else {
16037c478bd9Sstevel@tonic-gate 		printf(gettext("\nseal succeeded\n\n"));
16047c478bd9Sstevel@tonic-gate 		return;
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate }
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate static void
16097c478bd9Sstevel@tonic-gate _gss_unseal(argc, argv)
16107c478bd9Sstevel@tonic-gate int argc;
16117c478bd9Sstevel@tonic-gate char **argv;
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
16167c478bd9Sstevel@tonic-gate 	gss_ctx_id_t context_handle;
16177c478bd9Sstevel@tonic-gate 	gss_buffer_desc output_message_buffer;
16187c478bd9Sstevel@tonic-gate 	int conf_state;
16197c478bd9Sstevel@tonic-gate 	int qop_state;
16207c478bd9Sstevel@tonic-gate 	uid_t uid;
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/* set up the arguments specified in the input parameters */
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	if (argc == 0) {
16277c478bd9Sstevel@tonic-gate 		usage();
16287c478bd9Sstevel@tonic-gate 		return;
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "initiator") == 0)
16337c478bd9Sstevel@tonic-gate 		context_handle = initiator_context_handle;
16347c478bd9Sstevel@tonic-gate 	else if (strcmp(argv[0], "acceptor") == 0)
16357c478bd9Sstevel@tonic-gate 		context_handle = acceptor_context_handle;
16367c478bd9Sstevel@tonic-gate 	else {
16377c478bd9Sstevel@tonic-gate 		printf(gettext(
16387c478bd9Sstevel@tonic-gate 			"must specify either \"initiator\" or \"acceptor\"\n"));
16397c478bd9Sstevel@tonic-gate 		return;
16407c478bd9Sstevel@tonic-gate 	}
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	argc--;
16437c478bd9Sstevel@tonic-gate 	argv++;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	if (argc != 0) {
16467c478bd9Sstevel@tonic-gate 		usage();
16477c478bd9Sstevel@tonic-gate 		return;
16487c478bd9Sstevel@tonic-gate 	}
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	status = kgss_unseal(&minor_status,
16517c478bd9Sstevel@tonic-gate 			context_handle,
16527c478bd9Sstevel@tonic-gate 			&message_buffer,
16537c478bd9Sstevel@tonic-gate 			&output_message_buffer,
16547c478bd9Sstevel@tonic-gate 			&conf_state,
16557c478bd9Sstevel@tonic-gate 			&qop_state,
16567c478bd9Sstevel@tonic-gate 			uid);
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	/* store major and minor status for gss_display_status() call */
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	gss_major_code = status;
16617c478bd9Sstevel@tonic-gate 	gss_minor_code = minor_status;
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
16647c478bd9Sstevel@tonic-gate 		printf(gettext("\nunseal succeeded\n\n"));
16657c478bd9Sstevel@tonic-gate 		printf(gettext("unsealed message = \"%s\"\n\n"),
16667c478bd9Sstevel@tonic-gate 			output_message_buffer.value);
16677c478bd9Sstevel@tonic-gate 		if (conf_state)
16687c478bd9Sstevel@tonic-gate 			printf(gettext("confidentiality and integrity used\n"));
16697c478bd9Sstevel@tonic-gate 		else
16707c478bd9Sstevel@tonic-gate 			printf(gettext("only integrity used\n"));
16717c478bd9Sstevel@tonic-gate 		printf(gettext("quality of protection = %d\n\n"), qop_state);
16727c478bd9Sstevel@tonic-gate 		gss_release_buffer(&minor_status, &output_message_buffer);
16737c478bd9Sstevel@tonic-gate 	} else {
16747c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
16757c478bd9Sstevel@tonic-gate 			status, gettext("gss_unseal error"));
16767c478bd9Sstevel@tonic-gate 	}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	/* free the message buffer and return */
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	gss_release_buffer(&minor_status, &message_buffer);
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate static void
16847c478bd9Sstevel@tonic-gate _gss_display_status(argc, argv)
16857c478bd9Sstevel@tonic-gate int argc;
16867c478bd9Sstevel@tonic-gate char **argv;
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
16897c478bd9Sstevel@tonic-gate 	OM_uint32 minor_status;
16907c478bd9Sstevel@tonic-gate 	int status_type;
16917c478bd9Sstevel@tonic-gate 	int status_value;
16927c478bd9Sstevel@tonic-gate 	gss_OID mech_type = (gss_OID) 0;
16937c478bd9Sstevel@tonic-gate 	int message_context;
16947c478bd9Sstevel@tonic-gate 	gss_buffer_desc status_string;
16957c478bd9Sstevel@tonic-gate 	uid_t uid;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/* initialize message context to zero */
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	message_context = 0;
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	if (argc == 0) {
17047c478bd9Sstevel@tonic-gate 		printf(gettext("Assuming Kerberos V5 as the mechanism\n"));
17057c478bd9Sstevel@tonic-gate 		printf(gettext(
17067c478bd9Sstevel@tonic-gate 			"The mech OID 1.2.840.113554.1.2.2 will be used\n"));
17077c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid((char *)GSS_KRB5_MECH_OID);
17087c478bd9Sstevel@tonic-gate 	} else
17097c478bd9Sstevel@tonic-gate 		mech_type = gss_str2oid(argv[0]);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	if (mech_type == 0 || mech_type->length == 0) {
17127c478bd9Sstevel@tonic-gate 		printf(gettext("improperly formated mechanism OID\n"));
17137c478bd9Sstevel@tonic-gate 		return;
17147c478bd9Sstevel@tonic-gate 	}
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	/* Is this call for the major or minor status? */
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "major") == 0) {
17197c478bd9Sstevel@tonic-gate 		status_type = GSS_C_GSS_CODE;
17207c478bd9Sstevel@tonic-gate 		status_value = gss_major_code;
17217c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[0], "minor") == 0) {
17227c478bd9Sstevel@tonic-gate 		status_type = GSS_C_MECH_CODE;
17237c478bd9Sstevel@tonic-gate 		status_value = gss_minor_code;
17247c478bd9Sstevel@tonic-gate 	} else {
17257c478bd9Sstevel@tonic-gate 		printf(gettext("must specify either \"major\" or \"minor\"\n"));
17267c478bd9Sstevel@tonic-gate 		return;
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	argc--;
17307c478bd9Sstevel@tonic-gate 	argv++;
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	if (argc != 0) {
17337c478bd9Sstevel@tonic-gate 		usage();
17347c478bd9Sstevel@tonic-gate 		return;
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	status = kgss_display_status(&minor_status,
17387c478bd9Sstevel@tonic-gate 				status_value,
17397c478bd9Sstevel@tonic-gate 				status_type,
17407c478bd9Sstevel@tonic-gate 				mech_type,
17417c478bd9Sstevel@tonic-gate 				&message_context,
17427c478bd9Sstevel@tonic-gate 				&status_string,
17437c478bd9Sstevel@tonic-gate 				uid);
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
17467c478bd9Sstevel@tonic-gate 		printf(gettext("status =\n  %s\n\n"), status_string.value);
17477c478bd9Sstevel@tonic-gate 	} else if (status == GSS_S_BAD_MECH) {
17487c478bd9Sstevel@tonic-gate 		printf(gettext("invalide mechanism OID\n\n"));
17497c478bd9Sstevel@tonic-gate 	} else {
17507c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
17517c478bd9Sstevel@tonic-gate 			status, gettext("gss_display_status error"));
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17567c478bd9Sstevel@tonic-gate static void
17577c478bd9Sstevel@tonic-gate _gss_indicate_mechs(argc, argv)
17587c478bd9Sstevel@tonic-gate int argc;
17597c478bd9Sstevel@tonic-gate char **argv;
17607c478bd9Sstevel@tonic-gate {
17617c478bd9Sstevel@tonic-gate 	OM_UINT32 status;
17627c478bd9Sstevel@tonic-gate 	OM_UINT32 minor_status;
17637c478bd9Sstevel@tonic-gate 	gss_OID_set oid_set = GSS_C_NULL_OID_SET;
17647c478bd9Sstevel@tonic-gate 	uid_t uid;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	uid = (uid_t) getuid();
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/* set up input arguments here */
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	if (argc != 0) {
17717c478bd9Sstevel@tonic-gate 		usage();
17727c478bd9Sstevel@tonic-gate 		return;
17737c478bd9Sstevel@tonic-gate 	}
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	status = kgss_indicate_mechs(&minor_status, &oid_set, uid);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
17787c478bd9Sstevel@tonic-gate 		int i;
17797c478bd9Sstevel@tonic-gate 		char *string;
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 		printf(gettext("%d supported mechanism%s%s\n"), oid_set->count,
17827c478bd9Sstevel@tonic-gate 			(oid_set->count == 1) ? "" : "s",
17837c478bd9Sstevel@tonic-gate 			(oid_set->count > 0) ? ":" : "");
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		for (i = 0; i < oid_set->count; i++) {
17867c478bd9Sstevel@tonic-gate 			string = gss_oid2str(&oid_set->elements[i]);
17877c478bd9Sstevel@tonic-gate 			printf(gettext("\t%s\n"), string);
17887c478bd9Sstevel@tonic-gate 			FREE(string, ((oid_set->elements[i].length+1)*4)+1);
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 		printf("\n");
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	} else {
17937c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
17947c478bd9Sstevel@tonic-gate 			status, gettext("gss_indicate_mechs error"));
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if (oid_set)
17987c478bd9Sstevel@tonic-gate 		gss_release_oid_set_and_oids(&minor_status, &oid_set);
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18027c478bd9Sstevel@tonic-gate static void
18037c478bd9Sstevel@tonic-gate _gss_inquire_cred(argc, argv)
18047c478bd9Sstevel@tonic-gate int argc;
18057c478bd9Sstevel@tonic-gate char **argv;
18067c478bd9Sstevel@tonic-gate {
18077c478bd9Sstevel@tonic-gate 	/* set up input arguments here */
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	if (argc != 0) {
18107c478bd9Sstevel@tonic-gate 		usage();
18117c478bd9Sstevel@tonic-gate 		return;
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	/* this function is unimplemented. Call usage() and return */
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	printf(gettext("\nUnsupported function"));
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate static char hexChars[] = "0123456789ABCDEF";
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate static void
18237c478bd9Sstevel@tonic-gate _gssd_expname_to_unix_cred(argc, argv)
18247c478bd9Sstevel@tonic-gate int argc;
18257c478bd9Sstevel@tonic-gate char **argv;
18267c478bd9Sstevel@tonic-gate {
18277c478bd9Sstevel@tonic-gate 	OM_uint32 major;
18287c478bd9Sstevel@tonic-gate 	gss_buffer_desc expName;
18297c478bd9Sstevel@tonic-gate 	char krb5_root_name[] = "040100092A864886F712010202000000"
18307c478bd9Sstevel@tonic-gate 		"25000A2A864886F71201020101726F6F744053554E534F46"
18317c478bd9Sstevel@tonic-gate 		"542E454E472E53554E2E434F4D00";
18327c478bd9Sstevel@tonic-gate 	unsigned char *byteStr, *hexStr;
18337c478bd9Sstevel@tonic-gate 	uid_t uidOut, uidIn;
18347c478bd9Sstevel@tonic-gate 	gid_t *gids, gidOut;
18357c478bd9Sstevel@tonic-gate 	int gidsLen, i, newLen;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	/* set up the arguments */
18387c478bd9Sstevel@tonic-gate 	uidIn = (uid_t) getuid();
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	if (argc < 1) {
18417c478bd9Sstevel@tonic-gate 		printf(gettext(
18427c478bd9Sstevel@tonic-gate 			"Using principal name of root for krberos_v5\n"));
18437c478bd9Sstevel@tonic-gate 		expName.value = (void*)krb5_root_name;
18447c478bd9Sstevel@tonic-gate 		expName.length = strlen(krb5_root_name);
18457c478bd9Sstevel@tonic-gate 	} else {
18467c478bd9Sstevel@tonic-gate 		expName.value = (void*)argv[0];
18477c478bd9Sstevel@tonic-gate 		expName.length = strlen(argv[0]);
18487c478bd9Sstevel@tonic-gate 	}
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/* convert the name from hex to byte... */
18517c478bd9Sstevel@tonic-gate 	hexStr = (unsigned char *)expName.value;
18527c478bd9Sstevel@tonic-gate 	newLen = expName.length/2;
18537c478bd9Sstevel@tonic-gate 	byteStr = (unsigned char *)MALLOC(newLen+1);
18547c478bd9Sstevel@tonic-gate 	expName.value = (char *)byteStr;
18557c478bd9Sstevel@tonic-gate 	for (i = 0; i < expName.length; i += 2) {
18567c478bd9Sstevel@tonic-gate 		*byteStr = (strchr(hexChars, *hexStr++) - hexChars) << 4;
18577c478bd9Sstevel@tonic-gate 		*byteStr += (strchr(hexChars, *hexStr++) - hexChars);
18587c478bd9Sstevel@tonic-gate 		byteStr++;
18597c478bd9Sstevel@tonic-gate 	}
18607c478bd9Sstevel@tonic-gate 	expName.length = newLen;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	major = kgsscred_expname_to_unix_cred(&expName, &uidOut, &gidOut,
18637c478bd9Sstevel@tonic-gate 					&gids, &gidsLen, uidIn);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	FREE(expName.value, newLen);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	if (major == GSS_S_COMPLETE) {
18687c478bd9Sstevel@tonic-gate 		printf(gettext("uid = <%d>\tgid = <%d>\t"), uidOut, gidOut);
18697c478bd9Sstevel@tonic-gate 		if (gidsLen > 0)
18707c478bd9Sstevel@tonic-gate 			printf(gettext(" %d gids <"), gidsLen);
18717c478bd9Sstevel@tonic-gate 		else
18727c478bd9Sstevel@tonic-gate 			printf(gettext(
18737c478bd9Sstevel@tonic-gate 				" no supplementary group information\n"));
18747c478bd9Sstevel@tonic-gate 		for (i = 0; i < gidsLen; i++)
18757c478bd9Sstevel@tonic-gate 			printf(" %d ", gids[i]);
18767c478bd9Sstevel@tonic-gate 		if (gidsLen > 0) {
18777c478bd9Sstevel@tonic-gate 			printf(">\n");
18787c478bd9Sstevel@tonic-gate 			FREE(gids, gidsLen * sizeof (gid_t));
18797c478bd9Sstevel@tonic-gate 		}
18807c478bd9Sstevel@tonic-gate 	} else {
18817c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
18827c478bd9Sstevel@tonic-gate 			major, gettext("gsscred_expname_to_unix_cred"));
18837c478bd9Sstevel@tonic-gate 	}
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate static void
18877c478bd9Sstevel@tonic-gate _gssd_name_to_unix_cred(argc, argv)
18887c478bd9Sstevel@tonic-gate int argc;
18897c478bd9Sstevel@tonic-gate char **argv;
18907c478bd9Sstevel@tonic-gate {
18917c478bd9Sstevel@tonic-gate 	OM_uint32 major, minor;
18927c478bd9Sstevel@tonic-gate 	gss_name_t gssName;
18937c478bd9Sstevel@tonic-gate 	gss_buffer_desc gssBuf = GSS_C_EMPTY_BUFFER;
18947c478bd9Sstevel@tonic-gate 	int gidsLen, i;
18957c478bd9Sstevel@tonic-gate 	gid_t *gids, gidOut;
18967c478bd9Sstevel@tonic-gate 	uid_t uidOut, uid;
18977c478bd9Sstevel@tonic-gate 	char defaultPrincipal[] = "root";
18987c478bd9Sstevel@tonic-gate 	gss_OID mechType, nameType;
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	uid = getuid();
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	/* optional argument 1 - contains principal name */
19037c478bd9Sstevel@tonic-gate 	if (argc > 0) {
19047c478bd9Sstevel@tonic-gate 		gssBuf.value = (void *)argv[0];
19057c478bd9Sstevel@tonic-gate 		gssBuf.length = strlen((char *)argv[0]);
19067c478bd9Sstevel@tonic-gate 	} else {
19077c478bd9Sstevel@tonic-gate 		gssBuf.value = (void *)defaultPrincipal;
19087c478bd9Sstevel@tonic-gate 		gssBuf.length = strlen(defaultPrincipal);
19097c478bd9Sstevel@tonic-gate 	}
19107c478bd9Sstevel@tonic-gate 	printf(gettext(
19117c478bd9Sstevel@tonic-gate 		"Using <%s> as the principal name.\n"), (char *)gssBuf.value);
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	/* optional argument 2 - contains name oid */
19157c478bd9Sstevel@tonic-gate 	if (argc > 1)
19167c478bd9Sstevel@tonic-gate 		nameType = gss_str2oid((char *) argv[1]);
19177c478bd9Sstevel@tonic-gate 	else
19187c478bd9Sstevel@tonic-gate 		nameType = (gss_OID)GSS_C_NT_USER_NAME;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	if (nameType == NULL || nameType->length == 0) {
19217c478bd9Sstevel@tonic-gate 		printf(gettext("improperly formated name OID\n"));
19227c478bd9Sstevel@tonic-gate 		return;
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 	printf(gettext("Principal name of type: <%s>.\n"),
19257c478bd9Sstevel@tonic-gate 		(argc > 1) ? argv[1] : "GSS_C_NT_USER_NAME");
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/* optional argument 3 - contains mech oid */
19297c478bd9Sstevel@tonic-gate 	if (argc > 2)
19307c478bd9Sstevel@tonic-gate 		mechType = gss_str2oid(argv[2]);
19317c478bd9Sstevel@tonic-gate 	else
19327c478bd9Sstevel@tonic-gate 		mechType = gss_str2oid((char *)GSS_KRB5_MECH_OID);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if (mechType == NULL || mechType->length == NULL) {
19357c478bd9Sstevel@tonic-gate 		FREE(nameType->elements, nameType->length);
19367c478bd9Sstevel@tonic-gate 		FREE(nameType, sizeof (gss_OID_desc));
19377c478bd9Sstevel@tonic-gate 		printf(gettext("improperly formated mech OID\n"));
19387c478bd9Sstevel@tonic-gate 		return;
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 	printf(gettext("Mechanism oid: <%s>.\n"),
19417c478bd9Sstevel@tonic-gate 		(argc > 2) ? argv[2] :
19427c478bd9Sstevel@tonic-gate 		(char *)GSS_KRB5_MECH_OID "(Kerberos v5)");
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	/* convert the name to internal format */
19467c478bd9Sstevel@tonic-gate 	if ((major = gss_import_name(&minor, &gssBuf,
19477c478bd9Sstevel@tonic-gate 				nameType, &gssName)) != GSS_S_COMPLETE) {
19487c478bd9Sstevel@tonic-gate 		printf(gettext("could not parse name: err (octal) %o (%s)\n"),
19497c478bd9Sstevel@tonic-gate 			major, "gss_import_name");
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 		FREE(nameType->elements, nameType->length);
19527c478bd9Sstevel@tonic-gate 		FREE(nameType, sizeof (gss_OID_desc));
19537c478bd9Sstevel@tonic-gate 		return;
19547c478bd9Sstevel@tonic-gate 	}
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	major = kgsscred_name_to_unix_cred(gssName, mechType, &uidOut,
19577c478bd9Sstevel@tonic-gate 					&gidOut, &gids, &gidsLen, uid);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	gss_release_name(&minor, &gssName);
19607c478bd9Sstevel@tonic-gate 	FREE(mechType->elements, mechType->length);
19617c478bd9Sstevel@tonic-gate 	FREE(mechType, sizeof (gss_OID_desc));
19627c478bd9Sstevel@tonic-gate 	if (argc > 1) {
19637c478bd9Sstevel@tonic-gate 		FREE(nameType->elements, nameType->length);
19647c478bd9Sstevel@tonic-gate 		FREE(nameType, sizeof (gss_OID_desc));
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	if (major == GSS_S_COMPLETE) {
19687c478bd9Sstevel@tonic-gate 		printf("uid = <%d>\tgid = <%d>\t", uidOut, gidOut);
19697c478bd9Sstevel@tonic-gate 		if (gidsLen > 0)
19707c478bd9Sstevel@tonic-gate 			printf(gettext(" %d gids <"), gidsLen);
19717c478bd9Sstevel@tonic-gate 		else
19727c478bd9Sstevel@tonic-gate 			printf(gettext(
19737c478bd9Sstevel@tonic-gate 				" no supplementary group information\n"));
19747c478bd9Sstevel@tonic-gate 		for (i = 0; i < gidsLen; i++)
19757c478bd9Sstevel@tonic-gate 			printf(" %d ", gids[i]);
19767c478bd9Sstevel@tonic-gate 		if (gidsLen > 0) {
19777c478bd9Sstevel@tonic-gate 			printf(">\n");
19787c478bd9Sstevel@tonic-gate 			FREE(gids, gidsLen * sizeof (gid_t));
19797c478bd9Sstevel@tonic-gate 		}
19807c478bd9Sstevel@tonic-gate 	} else {
19817c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
19827c478bd9Sstevel@tonic-gate 			major, gettext("gsscred_name_to_unix_cred"));
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate }
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate static void
19877c478bd9Sstevel@tonic-gate _gssd_get_group_info(argc, argv)
19887c478bd9Sstevel@tonic-gate int argc;
19897c478bd9Sstevel@tonic-gate char **argv;
19907c478bd9Sstevel@tonic-gate {
19917c478bd9Sstevel@tonic-gate 	OM_uint32 major;
19927c478bd9Sstevel@tonic-gate 	uid_t puid, uidIn;
19937c478bd9Sstevel@tonic-gate 	gid_t *gids, gidOut;
19947c478bd9Sstevel@tonic-gate 	int gidsLen, i;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/* set up the arguments */
19977c478bd9Sstevel@tonic-gate 	uidIn = (uid_t) getuid();
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	if (argc < 1)
20007c478bd9Sstevel@tonic-gate 		puid = 0;
20017c478bd9Sstevel@tonic-gate 	else
20027c478bd9Sstevel@tonic-gate 		puid = atol(argv[0]);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	printf(gettext("Retrieving group info for uid of <%d>\n"), puid);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	major = kgss_get_group_info(puid, &gidOut, &gids, &gidsLen, uidIn);
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	if (major == GSS_S_COMPLETE) {
20097c478bd9Sstevel@tonic-gate 		printf(gettext("group id = <%d>\t"), gidOut);
20107c478bd9Sstevel@tonic-gate 		if (gidsLen > 0)
20117c478bd9Sstevel@tonic-gate 			printf(gettext(" %d gids <"), gidsLen);
20127c478bd9Sstevel@tonic-gate 		else
20137c478bd9Sstevel@tonic-gate 			printf(gettext(
20147c478bd9Sstevel@tonic-gate 				" no supplementary group information\n"));
20157c478bd9Sstevel@tonic-gate 		for (i = 0; i < gidsLen; i++)
20167c478bd9Sstevel@tonic-gate 			printf(" %d ", gids[i]);
20177c478bd9Sstevel@tonic-gate 		if (gidsLen > 0) {
20187c478bd9Sstevel@tonic-gate 			printf(">\n");
20197c478bd9Sstevel@tonic-gate 			FREE(gids, gidsLen * sizeof (gid_t));
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 	} else {
20227c478bd9Sstevel@tonic-gate 		printf(gettext("server ret err (octal) %o (%s)\n"),
20237c478bd9Sstevel@tonic-gate 			major, "gss_get_group_info");
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate }
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate static gss_OID
20287c478bd9Sstevel@tonic-gate gss_str2oid(string)
20297c478bd9Sstevel@tonic-gate char * string;
20307c478bd9Sstevel@tonic-gate {
20317c478bd9Sstevel@tonic-gate 	/*
20327c478bd9Sstevel@tonic-gate 	 * a convenient wrapper routine for gss_str_to_oid
20337c478bd9Sstevel@tonic-gate 	 * this can handle all valid oid strings.
20347c478bd9Sstevel@tonic-gate 	 */
20357c478bd9Sstevel@tonic-gate 	OM_uint32 minor;
20367c478bd9Sstevel@tonic-gate 	gss_buffer_desc abuf;
20377c478bd9Sstevel@tonic-gate 	gss_OID oidOut;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	abuf.value = (void*)string;
20407c478bd9Sstevel@tonic-gate 	abuf.length = strlen(string);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if (gss_str_to_oid(&minor, &abuf, &oidOut) != GSS_S_COMPLETE)
20437c478bd9Sstevel@tonic-gate 		return (NULL);
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	return (oidOut);
20467c478bd9Sstevel@tonic-gate }
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate static char *
20497c478bd9Sstevel@tonic-gate gss_oid2str(oid)
20507c478bd9Sstevel@tonic-gate gss_OID oid;
20517c478bd9Sstevel@tonic-gate {
20527c478bd9Sstevel@tonic-gate 	/*
20537c478bd9Sstevel@tonic-gate 	 * a convenient wrapper for gss_oid_to_str
20547c478bd9Sstevel@tonic-gate 	 * this calls the GSS-API routine which should
20557c478bd9Sstevel@tonic-gate 	 * be able to handle all types of oids.
20567c478bd9Sstevel@tonic-gate 	 */
20577c478bd9Sstevel@tonic-gate 	OM_uint32 minor;
20587c478bd9Sstevel@tonic-gate 	gss_buffer_desc oidStr;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	if (gss_oid_to_str(&minor, oid, &oidStr) != GSS_S_COMPLETE)
20617c478bd9Sstevel@tonic-gate 		return (NULL);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	return ((char *)oidStr.value);
20647c478bd9Sstevel@tonic-gate } /* gss_oid2str */
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate static void
20677c478bd9Sstevel@tonic-gate instructs()
20687c478bd9Sstevel@tonic-gate {
20697c478bd9Sstevel@tonic-gate 	fprintf(stderr,
20707c478bd9Sstevel@tonic-gate 		gettext(
20717c478bd9Sstevel@tonic-gate "\nThis program must be run as root. Root must be installed on the KDC\n"
20727c478bd9Sstevel@tonic-gate "and exist in srvtab as root/<hostname>, where <hostname> is the machine on\n"
20737c478bd9Sstevel@tonic-gate "which the test runs. Before running gssdtest for Kerberos mechanism, the\n"
20747c478bd9Sstevel@tonic-gate "operator running as root must kinit as some other principal, e.g., test.\n"
20757c478bd9Sstevel@tonic-gate "There are two mechanisms avaialble: dummy and Kerberos(default).\n"
20767c478bd9Sstevel@tonic-gate "The OID for dummy mechanism is 1.3.6.1.4.1.42.2.26.1.2.\n"
20777c478bd9Sstevel@tonic-gate "The OID for Kerberos mechanism is 1.2.840.113554.1.2.2.\n"
20787c478bd9Sstevel@tonic-gate "The order of context establishment calls is important. First, acquire must"
20797c478bd9Sstevel@tonic-gate "\nbe called. This obtains the credentials used by accept. Acquire need\n"
20807c478bd9Sstevel@tonic-gate "only be called once, since the credentials it returns are used each time\n"
20817c478bd9Sstevel@tonic-gate "accept is called. Then init is called, followed by accept. Calling init\n"
20827c478bd9Sstevel@tonic-gate "twice without calling accept or calling these in a different order gives\n"
20837c478bd9Sstevel@tonic-gate "erroneous results and will cause memory leaks in the gssapi daemon. \n"
20847c478bd9Sstevel@tonic-gate "Finally, after calling init and accept, init must be called again to\n"
20857c478bd9Sstevel@tonic-gate "finish context establishment. So an example sequence (with data valid for\n"
20867c478bd9Sstevel@tonic-gate "the Kerberos mechanism and running on the machine \"elrond\" in the realm\n"
20877c478bd9Sstevel@tonic-gate "FOO.BAR.SUN.COM is :\n"));
20887c478bd9Sstevel@tonic-gate 	fprintf(stderr,
20897c478bd9Sstevel@tonic-gate 		gettext("\nacquire service@host 1.2.840.113554.1.2.2\n"
20907c478bd9Sstevel@tonic-gate 		"init service@host 1.2.840.113554.1.2.2\n"
20917c478bd9Sstevel@tonic-gate 		"accept\ninit service@host 1.2.840.113554.1.2.2\n"
20927c478bd9Sstevel@tonic-gate 		"\nAfter a context is established, sign, seal,\n"
20937c478bd9Sstevel@tonic-gate 		"verify and unseal may be called. Here are some examples\n"
20947c478bd9Sstevel@tonic-gate 		"for these routines : \n\n"
20957c478bd9Sstevel@tonic-gate 		"sign initiator ThisTestMessageIsForSigning\n"
20967c478bd9Sstevel@tonic-gate 		"verify acceptor\nseal initiator ThisTestMessageIsForSealing\n"
20977c478bd9Sstevel@tonic-gate 		"unseal acceptor\n\nEach input line is terminated by <cr>.\n"
20987c478bd9Sstevel@tonic-gate 		"The program is terminated by cntl-d\nor the command \"exit\""
20997c478bd9Sstevel@tonic-gate 		"\nfrom the prompt\n\n"));
21007c478bd9Sstevel@tonic-gate }
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate static void
21037c478bd9Sstevel@tonic-gate usage()
21047c478bd9Sstevel@tonic-gate {
21057c478bd9Sstevel@tonic-gate 	fprintf(stderr,
21067c478bd9Sstevel@tonic-gate 		gettext(
21077c478bd9Sstevel@tonic-gate 		"\nusage:\t[acquire | gss_acquire_cred]"
21087c478bd9Sstevel@tonic-gate 		"desired_name mech_type\n"
21097c478bd9Sstevel@tonic-gate 		"\t[release | gss_release_cred]\n"
21107c478bd9Sstevel@tonic-gate 		"\t[init | gss_init_sec_context] target_name mech_type\n"
21117c478bd9Sstevel@tonic-gate 		"\t[accept | gss_accept_sec_context]\n"
21127c478bd9Sstevel@tonic-gate 		"\t[process | gss_process_context_token] initiator | acceptor\n"
21137c478bd9Sstevel@tonic-gate 		"\t[delete | gss_delete_sec_context] initiator | acceptor\n"
21147c478bd9Sstevel@tonic-gate 		"\t[time | gss_context_time] {not yet implemented}\n"
21157c478bd9Sstevel@tonic-gate 		"\t[sign | gss_sign] initiator | acceptor message-to-sign\n"
21167c478bd9Sstevel@tonic-gate 		"\t[verify | gss_verify] initiator | acceptor\n"
21177c478bd9Sstevel@tonic-gate 		"\t[seal | gss_seal] initiator | acceptor message-to-seal\n"
21187c478bd9Sstevel@tonic-gate 		"\t[unseal | gss_unseal] initiator | acceptor\n"
21197c478bd9Sstevel@tonic-gate 		"\t[status | gss_display_status] mech_type  [major | minor] \n"
21207c478bd9Sstevel@tonic-gate 		"\t[indicate | gss_indicate_mechs]\n"
21217c478bd9Sstevel@tonic-gate 		"\t[inquire | gss_inquire_cred] {not yet implemented}\n"
21227c478bd9Sstevel@tonic-gate 		"\t[expname2unixcred | gsscred_expname_to_unix_cred]"
21237c478bd9Sstevel@tonic-gate 		" export-name\n"
21247c478bd9Sstevel@tonic-gate 		"\t[name2unixcred | gsscred_name_to_unix_cred] "
21257c478bd9Sstevel@tonic-gate 		"pname [name_type mech_type]\n"
21267c478bd9Sstevel@tonic-gate 		"\t[grpinfo | gss_get_group_info] uid\n"
21277c478bd9Sstevel@tonic-gate 		"\t[gss_all | all] desired_name\n"
21287c478bd9Sstevel@tonic-gate 		"\t[gss_loop | loop] desired_name\n"
21297c478bd9Sstevel@tonic-gate 		"\texit\n\n"));
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate /* Copied from parse_argv(), then modified */
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate static int
21357c478bd9Sstevel@tonic-gate parse_input_line(input_line, argc, argv)
21367c478bd9Sstevel@tonic-gate char *input_line;
21377c478bd9Sstevel@tonic-gate int * argc;
21387c478bd9Sstevel@tonic-gate char ***argv;
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate 	const char nil = '\0';
21417c478bd9Sstevel@tonic-gate 	char * chptr;
21427c478bd9Sstevel@tonic-gate 	int chr_cnt;
21437c478bd9Sstevel@tonic-gate 	int arg_cnt = 0;
21447c478bd9Sstevel@tonic-gate 	int ch_was_space = 1;
21457c478bd9Sstevel@tonic-gate 	int ch_is_space;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	chr_cnt = strlen(input_line);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	/* Count the arguments in the input_line string */
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	*argc = 1;
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	for (chptr = &input_line[0]; *chptr != nil; chptr++) {
21547c478bd9Sstevel@tonic-gate 		ch_is_space = isspace(*chptr);
21557c478bd9Sstevel@tonic-gate 		if (ch_is_space && !ch_was_space) {
21567c478bd9Sstevel@tonic-gate 			(*argc)++;
21577c478bd9Sstevel@tonic-gate 		}
21587c478bd9Sstevel@tonic-gate 		ch_was_space = ch_is_space;
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	if (ch_was_space) {
21627c478bd9Sstevel@tonic-gate 		(*argc)--;
21637c478bd9Sstevel@tonic-gate 	}	/* minus trailing spaces */
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	/* Now that we know how many args calloc the argv array */
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	*argv = (char **) CALLOC((*argc)+1, sizeof (char *));
21687c478bd9Sstevel@tonic-gate 	chptr = (char *) (&input_line[0]);
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	for (ch_was_space = 1; *chptr != nil; chptr++) {
21717c478bd9Sstevel@tonic-gate 		ch_is_space = isspace(*chptr);
21727c478bd9Sstevel@tonic-gate 		if (ch_is_space) {
21737c478bd9Sstevel@tonic-gate 			*chptr = nil;	/* replace each space with nil	*/
21747c478bd9Sstevel@tonic-gate 		} else if (ch_was_space) {	/* begining of word? */
21757c478bd9Sstevel@tonic-gate 			(*argv)[arg_cnt++] = chptr;	/* new argument ? */
21767c478bd9Sstevel@tonic-gate 		}
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 		ch_was_space = ch_is_space;
21797c478bd9Sstevel@tonic-gate 	}
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	return (chr_cnt);
21827c478bd9Sstevel@tonic-gate }
2183