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