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