xref: /onnv-gate/usr/src/lib/krb5/kadm5/clnt/client_init.c (revision 1692:d9031cb1a5a8)
10Sstevel@tonic-gate /*
2*1692Ssemery  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * $Header: /cvs/krbdev/krb5/src/lib/kadm5/clnt/client_init.c,v 1.13.2.2 2000/05/09 13:17:14 raeburn Exp $
60Sstevel@tonic-gate  */
70Sstevel@tonic-gate 
80Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
90Sstevel@tonic-gate 
100Sstevel@tonic-gate /*
110Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * All rights reserved.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * Export of this software from the United States of America may require
160Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
170Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
180Sstevel@tonic-gate  * obtain such a license before exporting.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
210Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
220Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
230Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
240Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
250Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
260Sstevel@tonic-gate  * to distribution of the software without specific, written prior
270Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
280Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
290Sstevel@tonic-gate  * or implied warranty.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
320Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
330Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * $Header: /afs/athena.mit.edu/astaff/project/krbdev/.cvsroot/src/lib/kadm5/clnt/client_init.c,v 1.6 1996/11/07 17:13:44 tytso Exp $
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <netdb.h>
450Sstevel@tonic-gate #include <memory.h>
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include <com_err.h>
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <netinet/in.h>
510Sstevel@tonic-gate #include <krb5.h>
520Sstevel@tonic-gate #include <k5-int.h> /* for KRB5_ADM_DEFAULT_PORT */
530Sstevel@tonic-gate #ifdef __STDC__
540Sstevel@tonic-gate #include <stdlib.h>
550Sstevel@tonic-gate #endif
560Sstevel@tonic-gate #include <libintl.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <syslog.h>
590Sstevel@tonic-gate #include <gssapi/gssapi.h>
600Sstevel@tonic-gate #include <gssapi_krb5.h>
610Sstevel@tonic-gate #include <gssapiP_krb5.h>
620Sstevel@tonic-gate #include <kadm5/kadm_rpc.h>
630Sstevel@tonic-gate #include <rpc/clnt.h>
640Sstevel@tonic-gate #include <kadm5/admin.h>
650Sstevel@tonic-gate #include "client_internal.h"
660Sstevel@tonic-gate #include <iprop_hdr.h>
670Sstevel@tonic-gate #include "iprop.h"
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	ADM_CCACHE  "/tmp/ovsec_adm.XXXXXX"
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /* connection timeout to kadmind in seconds */
720Sstevel@tonic-gate #define		KADMIND_CONNECT_TIMEOUT	25
730Sstevel@tonic-gate 
740Sstevel@tonic-gate int _kadm5_check_handle();
750Sstevel@tonic-gate 
760Sstevel@tonic-gate enum init_type { INIT_PASS, INIT_SKEY, INIT_CREDS };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static kadm5_ret_t _kadm5_init_any(char *client_name,
790Sstevel@tonic-gate 				   enum init_type init_type,
800Sstevel@tonic-gate 				   char *pass,
810Sstevel@tonic-gate 				   krb5_ccache ccache_in,
820Sstevel@tonic-gate 				   char *service_name,
830Sstevel@tonic-gate 				   kadm5_config_params *params,
840Sstevel@tonic-gate 				   krb5_ui_4 struct_version,
850Sstevel@tonic-gate 				   krb5_ui_4 api_version,
860Sstevel@tonic-gate 				   void **server_handle);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate kadm5_ret_t kadm5_init_with_creds(char *client_name,
890Sstevel@tonic-gate 				  krb5_ccache ccache,
900Sstevel@tonic-gate 				  char *service_name,
910Sstevel@tonic-gate 				  kadm5_config_params *params,
920Sstevel@tonic-gate 				  krb5_ui_4 struct_version,
930Sstevel@tonic-gate 				  krb5_ui_4 api_version,
940Sstevel@tonic-gate 				  void **server_handle)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	return _kadm5_init_any(client_name, INIT_CREDS, NULL, ccache,
970Sstevel@tonic-gate 			    service_name, params,
980Sstevel@tonic-gate 			    struct_version, api_version,
990Sstevel@tonic-gate 			    server_handle);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate kadm5_ret_t kadm5_init_with_password(char *client_name, char *pass,
1040Sstevel@tonic-gate 				     char *service_name,
1050Sstevel@tonic-gate 				     kadm5_config_params *params,
1060Sstevel@tonic-gate 				     krb5_ui_4 struct_version,
1070Sstevel@tonic-gate 				     krb5_ui_4 api_version,
1080Sstevel@tonic-gate 				     void **server_handle)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	return _kadm5_init_any(client_name, INIT_PASS, pass, NULL,
1110Sstevel@tonic-gate 			    service_name, params, struct_version,
1120Sstevel@tonic-gate 			    api_version, server_handle);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate kadm5_ret_t kadm5_init(char *client_name, char *pass,
1160Sstevel@tonic-gate 			 char *service_name,
1170Sstevel@tonic-gate 			 kadm5_config_params *params,
1180Sstevel@tonic-gate 			 krb5_ui_4 struct_version,
1190Sstevel@tonic-gate 			 krb5_ui_4 api_version,
1200Sstevel@tonic-gate 			 void **server_handle)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	return _kadm5_init_any(client_name, INIT_PASS, pass, NULL,
1230Sstevel@tonic-gate 			    service_name, params, struct_version,
1240Sstevel@tonic-gate 			    api_version, server_handle);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate kadm5_ret_t kadm5_init_with_skey(char *client_name, char *keytab,
1280Sstevel@tonic-gate 				 char *service_name,
1290Sstevel@tonic-gate 				 kadm5_config_params *params,
1300Sstevel@tonic-gate 				 krb5_ui_4 struct_version,
1310Sstevel@tonic-gate 				 krb5_ui_4 api_version,
1320Sstevel@tonic-gate 				 void **server_handle)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	return _kadm5_init_any(client_name, INIT_SKEY, keytab, NULL,
1350Sstevel@tonic-gate 			    service_name, params, struct_version,
1360Sstevel@tonic-gate 			    api_version, server_handle);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate krb5_error_code  kadm5_free_config_params();
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static void
1420Sstevel@tonic-gate display_status_1(m, code, type, mech)
1430Sstevel@tonic-gate char *m;
1440Sstevel@tonic-gate OM_uint32 code;
1450Sstevel@tonic-gate int type;
1460Sstevel@tonic-gate const gss_OID mech;
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	OM_uint32 maj_stat, min_stat;
1490Sstevel@tonic-gate 	gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
1500Sstevel@tonic-gate 	OM_uint32 msg_ctx;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	msg_ctx = 0;
1530Sstevel@tonic-gate 	ADMIN_LOG(LOG_ERR, "%s\n", m);
1540Sstevel@tonic-gate 	/* LINTED */
1550Sstevel@tonic-gate 	while (1) {
1560Sstevel@tonic-gate 		maj_stat = gss_display_status(&min_stat, code,
1570Sstevel@tonic-gate 					    type, mech,
1580Sstevel@tonic-gate 					    &msg_ctx, &msg);
1590Sstevel@tonic-gate 		if (maj_stat != GSS_S_COMPLETE) {
1600Sstevel@tonic-gate 			syslog(LOG_ERR,
1610Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
1620Sstevel@tonic-gate 				    "error in gss_display_status"
1630Sstevel@tonic-gate 				    " called from <%s>\n"), m);
1640Sstevel@tonic-gate 			break;
1650Sstevel@tonic-gate 		} else
1660Sstevel@tonic-gate 			syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
1670Sstevel@tonic-gate 						"GSS-API error : %s\n"),
1680Sstevel@tonic-gate 			    m);
1690Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
1700Sstevel@tonic-gate 					"GSS-API error : %s\n"),
1710Sstevel@tonic-gate 		    (char *)msg.value);
1720Sstevel@tonic-gate 		if (msg.length != 0)
1730Sstevel@tonic-gate 			(void) gss_release_buffer(&min_stat, &msg);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		if (!msg_ctx)
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Function: display_status
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * Purpose: displays GSS-API messages
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  * Arguments:
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  * 	msg		a string to be displayed with the message
1880Sstevel@tonic-gate  * 	maj_stat	the GSS-API major status code
1890Sstevel@tonic-gate  * 	min_stat	the GSS-API minor status code
1900Sstevel@tonic-gate  *	mech		kerberos mech
1910Sstevel@tonic-gate  * Effects:
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  * The GSS-API messages associated with maj_stat and min_stat are
1940Sstevel@tonic-gate  * displayed on stderr, each preceeded by "GSS-API error <msg>: " and
1950Sstevel@tonic-gate  * followed by a newline.
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate void
1980Sstevel@tonic-gate display_status(msg, maj_stat, min_stat, mech)
1990Sstevel@tonic-gate char *msg;
2000Sstevel@tonic-gate OM_uint32 maj_stat;
2010Sstevel@tonic-gate OM_uint32 min_stat;
2020Sstevel@tonic-gate char *mech;
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	gss_OID mech_oid;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (!rpc_gss_mech_to_oid(mech, (rpc_gss_OID *)&mech_oid)) {
2070Sstevel@tonic-gate 		ADMIN_LOG(LOG_ERR,
2080Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN,
2090Sstevel@tonic-gate 				"Invalid mechanism oid <%s>"), mech);
2100Sstevel@tonic-gate 		return;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	display_status_1(msg, maj_stat, GSS_C_GSS_CODE, mech_oid);
2140Sstevel@tonic-gate 	display_status_1(msg, min_stat, GSS_C_MECH_CODE, mech_oid);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Open an fd for the given address and connect asynchronously. Wait
2190Sstevel@tonic-gate  * KADMIND_CONNECT_TIMEOUT seconds or till it succeeds. If it succeeds
2200Sstevel@tonic-gate  * change fd to blocking and return it, else return -1.
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate static int
2230Sstevel@tonic-gate get_connection(struct netconfig *nconf, struct netbuf netaddr)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	struct t_info tinfo;
2260Sstevel@tonic-gate 	struct t_call sndcall;
2270Sstevel@tonic-gate 	struct t_call *rcvcall = NULL;
2280Sstevel@tonic-gate 	int connect_time;
2290Sstevel@tonic-gate 	int flags;
2300Sstevel@tonic-gate 	int fd;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	(void) memset(&tinfo, 0, sizeof (tinfo));
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/* we'l open with O_NONBLOCK and avoid an fcntl */
2350Sstevel@tonic-gate 	fd = t_open(nconf->nc_device, O_RDWR | O_NONBLOCK, &tinfo);
2360Sstevel@tonic-gate 	if (fd == -1) {
2370Sstevel@tonic-gate 		return (-1);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (t_bind(fd, (struct t_bind *)NULL, (struct t_bind *)NULL) == -1) {
2410Sstevel@tonic-gate 		(void) close(fd);
2420Sstevel@tonic-gate 		return (-1);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/* we can't connect unless fd is in IDLE state */
2460Sstevel@tonic-gate 	if (t_getstate(fd) != T_IDLE) {
2470Sstevel@tonic-gate 		(void) close(fd);
2480Sstevel@tonic-gate 		return (-1);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/* setup connect parameters */
2520Sstevel@tonic-gate 	netaddr.len = netaddr.maxlen = __rpc_get_a_size(tinfo.addr);
2530Sstevel@tonic-gate 	sndcall.addr = netaddr;
2540Sstevel@tonic-gate 	sndcall.opt.len = sndcall.udata.len = 0;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/* we wait for KADMIND_CONNECT_TIMEOUT seconds from now */
2570Sstevel@tonic-gate 	connect_time = time(NULL) + KADMIND_CONNECT_TIMEOUT;
2580Sstevel@tonic-gate 	if (t_connect(fd, &sndcall, rcvcall) != 0) {
2590Sstevel@tonic-gate 		if (t_errno != TNODATA) {
2600Sstevel@tonic-gate 			(void) close(fd);
2610Sstevel@tonic-gate 			return (-1);
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/* loop till success or timeout */
2660Sstevel@tonic-gate 	for (;;) {
2670Sstevel@tonic-gate 		if (t_rcvconnect(fd, rcvcall) == 0)
2680Sstevel@tonic-gate 			break;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		if (t_errno != TNODATA || time(NULL) > connect_time) {
2710Sstevel@tonic-gate 			/* we have either timed out or caught an error */
2720Sstevel@tonic-gate 			(void) close(fd);
2730Sstevel@tonic-gate 			if (rcvcall != NULL)
2740Sstevel@tonic-gate 				t_free((char *)rcvcall, T_CALL);
2750Sstevel@tonic-gate 			return (-1);
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 		sleep(1);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/* make the fd blocking (synchronous) */
2810Sstevel@tonic-gate 	flags = fcntl(fd, F_GETFL, 0);
2820Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
2830Sstevel@tonic-gate 	if (rcvcall != NULL)
2840Sstevel@tonic-gate 		t_free((char *)rcvcall, T_CALL);
2850Sstevel@tonic-gate 	return (fd);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Open an RPCSEC_GSS connection and
2900Sstevel@tonic-gate  * get a client handle to use for future RPCSEC calls.
2910Sstevel@tonic-gate  *
2920Sstevel@tonic-gate  * This function is only used when changing passwords and
2930Sstevel@tonic-gate  * the kpasswd_protocol is RPCSEC_GSS
2940Sstevel@tonic-gate  */
2950Sstevel@tonic-gate static int
2960Sstevel@tonic-gate _kadm5_initialize_rpcsec_gss_handle(kadm5_server_handle_t handle,
2970Sstevel@tonic-gate 				    char *client_name,
2980Sstevel@tonic-gate 				    char *service_name)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	struct netbuf netaddr;
3010Sstevel@tonic-gate 	struct hostent *hp;
3020Sstevel@tonic-gate 	int fd;
3030Sstevel@tonic-gate 	struct sockaddr_in addr;
3040Sstevel@tonic-gate 	struct sockaddr_in *sin;
3050Sstevel@tonic-gate 	struct netconfig *nconf;
3060Sstevel@tonic-gate 	int code = 0;
3070Sstevel@tonic-gate 	generic_ret *r;
3080Sstevel@tonic-gate 	char *ccname_orig;
3090Sstevel@tonic-gate 	char *iprop_svc;
3100Sstevel@tonic-gate 	boolean_t iprop_enable = B_FALSE;
3110Sstevel@tonic-gate 	char mech[] = "kerberos_v5";
3120Sstevel@tonic-gate 	gss_OID mech_oid;
3130Sstevel@tonic-gate 	gss_OID_set_desc oid_set;
3140Sstevel@tonic-gate 	gss_name_t gss_client;
3150Sstevel@tonic-gate 	gss_buffer_desc input_name;
3160Sstevel@tonic-gate 	gss_cred_id_t gss_client_creds = GSS_C_NO_CREDENTIAL;
3170Sstevel@tonic-gate 	rpc_gss_options_req_t   options_req;
3180Sstevel@tonic-gate 	rpc_gss_options_ret_t   options_ret;
3190Sstevel@tonic-gate 	rpc_gss_service_t service = rpc_gss_svc_privacy;
3200Sstevel@tonic-gate 	OM_uint32 gssstat, minor_stat;
3210Sstevel@tonic-gate 	void *handlep;
3220Sstevel@tonic-gate 	enum clnt_stat rpc_err_code;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	hp = gethostbyname(handle->params.admin_server);
3250Sstevel@tonic-gate 	if (hp == (struct hostent *)NULL) {
3260Sstevel@tonic-gate 		code = KADM5_BAD_SERVER_NAME;
3270Sstevel@tonic-gate 		ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
3280Sstevel@tonic-gate 					    "bad server name\n"));
3290Sstevel@tonic-gate 		goto cleanup;
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	memset(&addr, 0, sizeof (addr));
3330Sstevel@tonic-gate 	addr.sin_family = hp->h_addrtype;
3340Sstevel@tonic-gate 	(void) memcpy((char *)&addr.sin_addr, (char *)hp->h_addr,
3350Sstevel@tonic-gate 		    sizeof (addr.sin_addr));
3360Sstevel@tonic-gate 	addr.sin_port = htons((ushort_t)handle->params.kadmind_port);
3370Sstevel@tonic-gate 	sin = &addr;
3380Sstevel@tonic-gate #ifdef DEBUG
3390Sstevel@tonic-gate 	printf("kadmin_port %d\n", handle->params.kadmind_port);
3400Sstevel@tonic-gate 	printf("addr: sin_port: %d, sin_family: %d, sin_zero %s\n",
3410Sstevel@tonic-gate 	    addr.sin_port, addr.sin_family, addr.sin_zero);
3420Sstevel@tonic-gate 	printf("sin_addr %d:%d\n", addr.sin_addr.S_un.S_un_w.s_w1,
3430Sstevel@tonic-gate 	    addr.sin_addr.S_un.S_un_w.s_w2);
3440Sstevel@tonic-gate #endif
3450Sstevel@tonic-gate 	if ((handlep = setnetconfig()) == (void *) NULL) {
3460Sstevel@tonic-gate 		(void) syslog(LOG_ERR,
3470Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN,
3480Sstevel@tonic-gate 				    "cannot get any transport information"));
3490Sstevel@tonic-gate 		goto error;
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	while (nconf = getnetconfig(handlep)) {
3530Sstevel@tonic-gate 		if ((nconf->nc_semantics == NC_TPI_COTS_ORD) &&
3540Sstevel@tonic-gate 		    (strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
3550Sstevel@tonic-gate 		    (strcmp(nconf->nc_proto, NC_TCP) == 0))
3560Sstevel@tonic-gate 			break;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (nconf == (struct netconfig *)NULL)
3600Sstevel@tonic-gate 		goto error;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/* Transform addr to netbuf */
3630Sstevel@tonic-gate 	(void) memset(&netaddr, 0, sizeof (netaddr));
3640Sstevel@tonic-gate 	netaddr.buf = (char *)sin;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/* get an fd connected to the given address */
3670Sstevel@tonic-gate 	fd =  get_connection(nconf, netaddr);
3680Sstevel@tonic-gate 	if (fd == -1) {
3690Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
3700Sstevel@tonic-gate 			"unable to open connection to ADMIN server "
3710Sstevel@tonic-gate 			"(t_error %i)"), t_errno);
3720Sstevel@tonic-gate 		code = KADM5_RPC_ERROR;
3730Sstevel@tonic-gate 		goto error;
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate #ifdef DEBUG
3770Sstevel@tonic-gate 	printf("fd: %d, KADM: %d, KADMVERS %d\n", fd, KADM, KADMVERS);
3780Sstevel@tonic-gate 	printf("nconf: nc_netid: %s, nc_semantics: %d, nc_flag: %d, "
3790Sstevel@tonic-gate 	    "nc_protofmly: %s\n",
3800Sstevel@tonic-gate 	    nconf->nc_netid, nconf->nc_semantics, nconf->nc_flag,
3810Sstevel@tonic-gate 	    nconf->nc_protofmly);
3820Sstevel@tonic-gate 	printf("nc_proto: %s, nc_device: %s, nc_nlookups: %d, nc_used: %d\n",
3830Sstevel@tonic-gate 	    nconf->nc_proto, nconf->nc_device, nconf->nc_nlookups,
3840Sstevel@tonic-gate 	    nconf->nc_unused);
3850Sstevel@tonic-gate 	printf("netaddr: maxlen %d, buf: %s, len: %d\n", netaddr.maxlen,
3860Sstevel@tonic-gate 	    netaddr.buf, netaddr.len);
3870Sstevel@tonic-gate #endif
3880Sstevel@tonic-gate  	/*
3890Sstevel@tonic-gate 	 * Tell clnt_tli_create that given fd is already connected
3900Sstevel@tonic-gate 	 *
3910Sstevel@tonic-gate 	 * If the service_name and client_name are iprop-centric,
3920Sstevel@tonic-gate 	 * we need to clnt_tli_create to the appropriate RPC prog
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 	iprop_svc = strdup(KIPROP_SVC_NAME);
3950Sstevel@tonic-gate 	if (iprop_svc == NULL)
3960Sstevel@tonic-gate 		return (ENOMEM);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if ((strstr(service_name, iprop_svc) != NULL) &&
3990Sstevel@tonic-gate 	    (strstr(client_name, iprop_svc) != NULL)) {
4000Sstevel@tonic-gate 		iprop_enable = B_TRUE;
4010Sstevel@tonic-gate 		handle->clnt = clnt_tli_create(fd, nconf, NULL,
4020Sstevel@tonic-gate 				    KRB5_IPROP_PROG, KRB5_IPROP_VERS, 0, 0);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 	else
4050Sstevel@tonic-gate 		handle->clnt = clnt_tli_create(fd, nconf, NULL,
4060Sstevel@tonic-gate 				    KADM, KADMVERS, 0, 0);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (iprop_svc)
4090Sstevel@tonic-gate 		free(iprop_svc);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if (handle->clnt == NULL) {
4120Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
4130Sstevel@tonic-gate 					"clnt_tli_create failed\n"));
4140Sstevel@tonic-gate 		code = KADM5_RPC_ERROR;
4150Sstevel@tonic-gate 		(void) close(fd);
4160Sstevel@tonic-gate 		goto error;
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * The rpc-handle was created on an fd opened and connected
4200Sstevel@tonic-gate 	 * by us, so we have to explicitly tell rpc to close it.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	if (clnt_control(handle->clnt, CLSET_FD_CLOSE, NULL) != TRUE) {
4230Sstevel@tonic-gate 		clnt_pcreateerror("ERROR:");
4240Sstevel@tonic-gate 		syslog(LOG_ERR, dgettext(TEXT_DOMAIN,
4250Sstevel@tonic-gate 			"clnt_control failed to set CLSET_FD_CLOSE"));
4260Sstevel@tonic-gate 		code = KADM5_RPC_ERROR;
4270Sstevel@tonic-gate 		(void) close(fd);
4280Sstevel@tonic-gate 		goto error;
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	handle->lhandle->clnt = handle->clnt;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/* now that handle->clnt is set, we can check the handle */
4340Sstevel@tonic-gate 	if (code = _kadm5_check_handle((void *) handle))
4350Sstevel@tonic-gate 		goto error;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * The RPC connection is open; establish the GSS-API
4390Sstevel@tonic-gate 	 * authentication context.
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 	ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
4420Sstevel@tonic-gate 				    "have an rpc connection open\n"));
4430Sstevel@tonic-gate 	/* use the kadm5 cache */
4440Sstevel@tonic-gate 	ccname_orig = getenv("KRB5CCNAME");
4450Sstevel@tonic-gate 	if (ccname_orig)
4460Sstevel@tonic-gate 		ccname_orig = strdup(ccname_orig);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	(void) krb5_setenv("KRB5CCNAME", handle->cache_name, 1);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	ADMIN_LOG(LOG_ERR,
4510Sstevel@tonic-gate 		dgettext(TEXT_DOMAIN,
4520Sstevel@tonic-gate 			"current credential cache: %s"), handle->cache_name);
4530Sstevel@tonic-gate 	input_name.value = client_name;
4540Sstevel@tonic-gate 	input_name.length = strlen((char *)input_name.value) + 1;
4550Sstevel@tonic-gate 	gssstat = gss_import_name(&minor_stat, &input_name,
4560Sstevel@tonic-gate 				(gss_OID)gss_nt_krb5_name, &gss_client);
4570Sstevel@tonic-gate 	if (gssstat != GSS_S_COMPLETE) {
4580Sstevel@tonic-gate 		code = KADM5_GSS_ERROR;
4590Sstevel@tonic-gate 		ADMIN_LOGO(LOG_ERR,
4600Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN,
4610Sstevel@tonic-gate 				"gss_import_name failed for client name\n"));
4620Sstevel@tonic-gate 		goto error;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (!rpc_gss_mech_to_oid(mech, (rpc_gss_OID *)&mech_oid)) {
4660Sstevel@tonic-gate 		ADMIN_LOG(LOG_ERR,
4670Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN,
4680Sstevel@tonic-gate 				"Invalid mechanism oid <%s>"), mech);
4690Sstevel@tonic-gate 		goto error;
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	oid_set.count = 1;
4730Sstevel@tonic-gate 	oid_set.elements = mech_oid;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	gssstat = gss_acquire_cred(&minor_stat, gss_client, 0,
4760Sstevel@tonic-gate 				&oid_set, GSS_C_INITIATE,
4770Sstevel@tonic-gate 				&gss_client_creds, NULL, NULL);
4780Sstevel@tonic-gate 	(void) gss_release_name(&minor_stat, &gss_client);
4790Sstevel@tonic-gate 	if (gssstat != GSS_S_COMPLETE) {
4800Sstevel@tonic-gate 		code = KADM5_GSS_ERROR;
4810Sstevel@tonic-gate 		ADMIN_LOG(LOG_ERR,
4820Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN,
4830Sstevel@tonic-gate 				"could not acquire credentials, "
4840Sstevel@tonic-gate 				"major error code: %d\n"), gssstat);
4850Sstevel@tonic-gate 		goto error;
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	handle->my_cred = gss_client_creds;
4880Sstevel@tonic-gate 	options_req.my_cred = gss_client_creds;
4890Sstevel@tonic-gate 	options_req.req_flags = GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG;
4900Sstevel@tonic-gate 	options_req.time_req = 0;
4910Sstevel@tonic-gate 	options_req.input_channel_bindings = NULL;
4920Sstevel@tonic-gate #ifndef INIT_TEST
4930Sstevel@tonic-gate 	handle->clnt->cl_auth = rpc_gss_seccreate(handle->clnt,
4940Sstevel@tonic-gate 						service_name,
4950Sstevel@tonic-gate 						mech,
4960Sstevel@tonic-gate 						service,
4970Sstevel@tonic-gate 						NULL,
4980Sstevel@tonic-gate 						&options_req,
4990Sstevel@tonic-gate 						&options_ret);
5000Sstevel@tonic-gate #endif /* ! INIT_TEST */
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if (ccname_orig) {
5030Sstevel@tonic-gate 		(void) krb5_setenv("KRB5CCNAME", ccname_orig, 1);
5040Sstevel@tonic-gate 		free(ccname_orig);
5050Sstevel@tonic-gate 	} else
5060Sstevel@tonic-gate 		(void) krb5_unsetenv("KRB5CCNAME");
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if (handle->clnt->cl_auth == NULL) {
5090Sstevel@tonic-gate 		code = KADM5_GSS_ERROR;
5100Sstevel@tonic-gate 		display_status(dgettext(TEXT_DOMAIN,
5110Sstevel@tonic-gate 					"rpc_gss_seccreate failed\n"),
5120Sstevel@tonic-gate 			    options_ret.major_status,
5130Sstevel@tonic-gate 			    options_ret.minor_status,
5140Sstevel@tonic-gate 			    mech);
5150Sstevel@tonic-gate 		goto error;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	/*
5190Sstevel@tonic-gate 	 * Bypass the remainder of the code and return straightaway
5200Sstevel@tonic-gate 	 * if the gss service requested is kiprop
5210Sstevel@tonic-gate 	 */
5220Sstevel@tonic-gate 	if (iprop_enable == B_TRUE) {
5230Sstevel@tonic-gate 		code = 0;
5240Sstevel@tonic-gate 		goto cleanup;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	r = init_1(&handle->api_version, handle->clnt, &rpc_err_code);
5280Sstevel@tonic-gate 	if (r == NULL) {
5290Sstevel@tonic-gate 		ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
5300Sstevel@tonic-gate 			"error during admin api initialization\n"));
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 		if (rpc_err_code == RPC_CANTENCODEARGS) {
5330Sstevel@tonic-gate 			ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
5340Sstevel@tonic-gate 				"encryption needed to encode RPC data may not be "
5350Sstevel@tonic-gate 				"installed/configured on this system"));
5360Sstevel@tonic-gate 			code = KADM5_RPC_ERROR_CANTENCODEARGS;
5370Sstevel@tonic-gate 		} else if (rpc_err_code == RPC_CANTDECODEARGS) {
5380Sstevel@tonic-gate 			ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
5390Sstevel@tonic-gate 				"encryption needed to decode RPC data may not be "
5400Sstevel@tonic-gate 				"installed/configured on the server"));
5410Sstevel@tonic-gate 			code = KADM5_RPC_ERROR_CANTDECODEARGS;
5420Sstevel@tonic-gate 		} else
5430Sstevel@tonic-gate 			code = KADM5_RPC_ERROR;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		goto error;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	if (r->code) {
5490Sstevel@tonic-gate 		code = r->code;
5500Sstevel@tonic-gate 		ADMIN_LOG(LOG_ERR,
5510Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN,
5520Sstevel@tonic-gate 				"error during admin api initialization: %d\n"),
5530Sstevel@tonic-gate 			r->code);
5540Sstevel@tonic-gate 		goto error;
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate error:
5570Sstevel@tonic-gate cleanup:
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (handlep != (void *) NULL)
5600Sstevel@tonic-gate 		(void) endnetconfig(handlep);
5610Sstevel@tonic-gate 	/*
5620Sstevel@tonic-gate 	 * gss_client_creds is freed only when there is an error condition,
5630Sstevel@tonic-gate 	 * given that rpc_gss_seccreate() will assign the cred pointer to the
5640Sstevel@tonic-gate 	 * my_cred member in the auth handle's private data structure.
5650Sstevel@tonic-gate 	 */
5660Sstevel@tonic-gate 	if (code && (gss_client_creds != GSS_C_NO_CREDENTIAL))
5670Sstevel@tonic-gate 		(void) gss_release_cred(&minor_stat, &gss_client_creds);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	return (code);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate static kadm5_ret_t _kadm5_init_any(char *client_name,
5730Sstevel@tonic-gate 				   enum init_type init_type,
5740Sstevel@tonic-gate 				   char *pass,
5750Sstevel@tonic-gate 				   krb5_ccache ccache_in,
5760Sstevel@tonic-gate 				   char *service_name,
5770Sstevel@tonic-gate 				   kadm5_config_params *params_in,
5780Sstevel@tonic-gate 				   krb5_ui_4 struct_version,
5790Sstevel@tonic-gate 				   krb5_ui_4 api_version,
5800Sstevel@tonic-gate 				   void **server_handle)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	int i;
5830Sstevel@tonic-gate 	krb5_creds	creds;
5840Sstevel@tonic-gate 	krb5_ccache ccache = NULL;
5850Sstevel@tonic-gate 	krb5_timestamp  now;
5860Sstevel@tonic-gate 	OM_uint32 gssstat, minor_stat;
5870Sstevel@tonic-gate 	kadm5_server_handle_t handle;
5880Sstevel@tonic-gate 	kadm5_config_params params_local;
5890Sstevel@tonic-gate 	int code = 0;
5900Sstevel@tonic-gate 	krb5_get_init_creds_opt opt;
5910Sstevel@tonic-gate 	gss_buffer_desc input_name;
5920Sstevel@tonic-gate 	krb5_error_code kret;
5930Sstevel@tonic-gate 	krb5_int32 starttime;
5940Sstevel@tonic-gate 	char *server = NULL;
5950Sstevel@tonic-gate 	krb5_principal serverp = NULL, clientp = NULL;
59696Ssemery 	bool_t cpw = FALSE;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
5990Sstevel@tonic-gate 		"entering kadm5_init_any\n"));
6000Sstevel@tonic-gate 	if (! server_handle) {
6010Sstevel@tonic-gate 		return (EINVAL);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if (! (handle = malloc(sizeof(*handle)))) {
6050Sstevel@tonic-gate 		return (ENOMEM);
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 	if (! (handle->lhandle = malloc(sizeof(*handle)))) {
6080Sstevel@tonic-gate 		free(handle);
6090Sstevel@tonic-gate 		return (ENOMEM);
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	handle->magic_number = KADM5_SERVER_HANDLE_MAGIC;
6130Sstevel@tonic-gate 	handle->struct_version = struct_version;
6140Sstevel@tonic-gate 	handle->api_version = api_version;
6150Sstevel@tonic-gate 	handle->clnt = 0;
6160Sstevel@tonic-gate 	handle->cache_name = 0;
6170Sstevel@tonic-gate 	handle->destroy_cache = 0;
6180Sstevel@tonic-gate 	*handle->lhandle = *handle;
6190Sstevel@tonic-gate 	handle->lhandle->api_version = KADM5_API_VERSION_2;
6200Sstevel@tonic-gate 	handle->lhandle->struct_version = KADM5_STRUCT_VERSION;
6210Sstevel@tonic-gate 	handle->lhandle->lhandle = handle->lhandle;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	kret = krb5_init_context(&handle->context);
6240Sstevel@tonic-gate 	if (kret) {
6250Sstevel@tonic-gate 		free(handle->lhandle);
6260Sstevel@tonic-gate 		free(handle);
6270Sstevel@tonic-gate 		return (kret);
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if(service_name == NULL || client_name == NULL) {
6310Sstevel@tonic-gate 		krb5_free_context(handle->context);
6320Sstevel@tonic-gate 		free(handle->lhandle);
6330Sstevel@tonic-gate 		free(handle);
6340Sstevel@tonic-gate 		return (EINVAL);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 	memset((char *) &creds, 0, sizeof(creds));
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * Verify the version numbers before proceeding; we can't use
6400Sstevel@tonic-gate 	 * CHECK_HANDLE because not all fields are set yet.
6410Sstevel@tonic-gate 	 */
6420Sstevel@tonic-gate 	GENERIC_CHECK_HANDLE(handle, KADM5_OLD_LIB_API_VERSION,
6430Sstevel@tonic-gate 			  KADM5_NEW_LIB_API_VERSION);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/*
6460Sstevel@tonic-gate 	 * Acquire relevant profile entries.  In version 2, merge values
6470Sstevel@tonic-gate 	 * in params_in with values from profile, based on
6480Sstevel@tonic-gate 	 * params_in->mask.
6490Sstevel@tonic-gate 	 *
6500Sstevel@tonic-gate 	 * In version 1, we've given a realm (which may be NULL) instead
6510Sstevel@tonic-gate 	 * of params_in.  So use that realm, make params_in contain an
6520Sstevel@tonic-gate 	 * empty mask, and behave like version 2.
6530Sstevel@tonic-gate 	 */
6540Sstevel@tonic-gate 	memset((char *) &params_local, 0, sizeof(params_local));
6550Sstevel@tonic-gate 	if (api_version == KADM5_API_VERSION_1) {
6560Sstevel@tonic-gate 		if (params_in)
6570Sstevel@tonic-gate 			params_local.mask = KADM5_CONFIG_REALM;
6580Sstevel@tonic-gate 		params_in = &params_local;
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate #define ILLEGAL_PARAMS ( \
6620Sstevel@tonic-gate 	KADM5_CONFIG_ACL_FILE	| KADM5_CONFIG_ADB_LOCKFILE | \
6630Sstevel@tonic-gate 	KADM5_CONFIG_DBNAME	| KADM5_CONFIG_ADBNAME | \
6640Sstevel@tonic-gate 	KADM5_CONFIG_DICT_FILE	| KADM5_CONFIG_ADMIN_KEYTAB | \
6650Sstevel@tonic-gate 	KADM5_CONFIG_STASH_FILE | KADM5_CONFIG_MKEY_NAME | \
6660Sstevel@tonic-gate 	KADM5_CONFIG_ENCTYPE	| KADM5_CONFIG_MAX_LIFE	| \
6670Sstevel@tonic-gate 	KADM5_CONFIG_MAX_RLIFE	| KADM5_CONFIG_EXPIRATION | \
6680Sstevel@tonic-gate 	KADM5_CONFIG_FLAGS	| KADM5_CONFIG_ENCTYPES	| \
6690Sstevel@tonic-gate 	KADM5_CONFIG_MKEY_FROM_KBD)
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (params_in && params_in->mask & ILLEGAL_PARAMS) {
6720Sstevel@tonic-gate 		krb5_free_context(handle->context);
6730Sstevel@tonic-gate 		free(handle->lhandle);
6740Sstevel@tonic-gate 		free(handle);
6750Sstevel@tonic-gate 		ADMIN_LOG(LOG_ERR, dgettext(TEXT_DOMAIN,
6760Sstevel@tonic-gate 			"bad client parameters, returning %d"),
6770Sstevel@tonic-gate 			KADM5_BAD_CLIENT_PARAMS);
6780Sstevel@tonic-gate 		return (KADM5_BAD_CLIENT_PARAMS);
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	if ((code = kadm5_get_config_params(handle->context,
6820Sstevel@tonic-gate 					DEFAULT_PROFILE_PATH,
6830Sstevel@tonic-gate 					"KRB5_CONFIG",
6840Sstevel@tonic-gate 					params_in,
6850Sstevel@tonic-gate 					&handle->params))) {
6860Sstevel@tonic-gate 		krb5_free_context(handle->context);
6870Sstevel@tonic-gate 		free(handle->lhandle);
6880Sstevel@tonic-gate 		free(handle);
6890Sstevel@tonic-gate 		ADMIN_LOG(LOG_ERR, dgettext(TEXT_DOMAIN,
6900Sstevel@tonic-gate 			"failed to get config_params, return: %d\n"), code);
6910Sstevel@tonic-gate 		return(code);
6920Sstevel@tonic-gate 	}
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate #define REQUIRED_PARAMS (KADM5_CONFIG_REALM | \
6950Sstevel@tonic-gate 			 KADM5_CONFIG_ADMIN_SERVER | \
6960Sstevel@tonic-gate 			 KADM5_CONFIG_KADMIND_PORT)
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if ((handle->params.mask & REQUIRED_PARAMS) != REQUIRED_PARAMS) {
6990Sstevel@tonic-gate 		(void) kadm5_free_config_params(handle->context,
7000Sstevel@tonic-gate 						&handle->params);
7010Sstevel@tonic-gate 		krb5_free_context(handle->context);
7020Sstevel@tonic-gate 		free(handle->lhandle);
7030Sstevel@tonic-gate 		free(handle);
7040Sstevel@tonic-gate 		ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
7050Sstevel@tonic-gate 			"missing config parameters\n"));
7060Sstevel@tonic-gate 		return (KADM5_MISSING_CONF_PARAMS);
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	/*
7100Sstevel@tonic-gate 	 * Acquire a service ticket for service_name@realm in the name of
7110Sstevel@tonic-gate 	 * client_name, using password pass (which could be NULL), and
7120Sstevel@tonic-gate 	 * create a ccache to store them in.  If INIT_CREDS, use the
7130Sstevel@tonic-gate 	 * ccache we were provided instead.
7140Sstevel@tonic-gate 	 */
7150Sstevel@tonic-gate 	if ((code = krb5_parse_name(handle->context, client_name,
7160Sstevel@tonic-gate 			    &creds.client))) {
7170Sstevel@tonic-gate 		ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
7180Sstevel@tonic-gate 			    "could not parse client name\n"));
7190Sstevel@tonic-gate 		goto error;
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 	clientp = creds.client;
7220Sstevel@tonic-gate 
72396Ssemery 	if (strncmp(service_name, KADM5_CHANGEPW_HOST_SERVICE,
72496Ssemery 	    strlen(KADM5_CHANGEPW_HOST_SERVICE)) == 0)
72596Ssemery 		cpw = TRUE;
72696Ssemery 
7270Sstevel@tonic-gate 	if (init_type == INIT_PASS &&
72896Ssemery 	    handle->params.kpasswd_protocol == KRB5_CHGPWD_CHANGEPW_V2 &&
72996Ssemery 	    cpw == TRUE) {
7300Sstevel@tonic-gate 		/*
7310Sstevel@tonic-gate 		 * The 'service_name' is constructed by the caller
7320Sstevel@tonic-gate 		 * but its done before the parameter which determines
7330Sstevel@tonic-gate 		 * the kpasswd_protocol is found.  The servers that
7340Sstevel@tonic-gate 		 * support the SET/CHANGE password protocol expect
7350Sstevel@tonic-gate 		 * a slightly different service principal than
7360Sstevel@tonic-gate 		 * the normal SEAM kadmind so construct the correct
7370Sstevel@tonic-gate 		 * name here and then forget it.
7380Sstevel@tonic-gate 		 */
7390Sstevel@tonic-gate 		char *newsvcname = NULL;
7400Sstevel@tonic-gate 		newsvcname = malloc(strlen(KADM5_CHANGEPW_SERVICE) +
7410Sstevel@tonic-gate 				    strlen(handle->params.realm) + 2);
7420Sstevel@tonic-gate 		if (newsvcname == NULL) {
7430Sstevel@tonic-gate 			return (ENOMEM);
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 		sprintf(newsvcname, "%s@%s", KADM5_CHANGEPW_SERVICE,
7460Sstevel@tonic-gate 			handle->params.realm);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		if ((code = krb5_parse_name(handle->context, newsvcname,
7490Sstevel@tonic-gate 					    &creds.server))) {
7500Sstevel@tonic-gate 			ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
7510Sstevel@tonic-gate 					    "could not parse server "
7520Sstevel@tonic-gate 					    "name\n"));
7530Sstevel@tonic-gate 			free(newsvcname);
7540Sstevel@tonic-gate 			goto error;
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 		free(newsvcname);
7570Sstevel@tonic-gate 	} else {
7580Sstevel@tonic-gate 		input_name.value = service_name;
7590Sstevel@tonic-gate 		input_name.length = strlen((char *)input_name.value) + 1;
7600Sstevel@tonic-gate 		gssstat = krb5_gss_import_name(handle->context,
7610Sstevel@tonic-gate 				    &minor_stat,
7620Sstevel@tonic-gate 				    &input_name,
7630Sstevel@tonic-gate 				    (gss_OID)GSS_C_NT_HOSTBASED_SERVICE,
7640Sstevel@tonic-gate 				    (gss_name_t *)&creds.server);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		if (gssstat != GSS_S_COMPLETE) {
7670Sstevel@tonic-gate 			code = KADM5_GSS_ERROR;
7680Sstevel@tonic-gate 			ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
7690Sstevel@tonic-gate 				"gss_import_name failed for client name\n"));
7700Sstevel@tonic-gate 			goto error;
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 	serverp = creds.server;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/* XXX temporarily fix a bug in krb5_cc_get_type */
7760Sstevel@tonic-gate #undef krb5_cc_get_type
7770Sstevel@tonic-gate #define krb5_cc_get_type(context, cache) ((cache)->ops->prefix)
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	if (init_type == INIT_CREDS) {
7800Sstevel@tonic-gate 		ccache = ccache_in;
7810Sstevel@tonic-gate 		handle->cache_name = (char *)
7820Sstevel@tonic-gate 			malloc(strlen(krb5_cc_get_type(handle->context, ccache)) +
7830Sstevel@tonic-gate 		    	strlen(krb5_cc_get_name(handle->context, ccache)) + 2);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 		if (handle->cache_name == NULL) {
7860Sstevel@tonic-gate 			code = ENOMEM;
7870Sstevel@tonic-gate 			goto error;
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 		sprintf(handle->cache_name, "%s:%s",
7900Sstevel@tonic-gate 			krb5_cc_get_type(handle->context, ccache),
7910Sstevel@tonic-gate 			krb5_cc_get_name(handle->context, ccache));
7920Sstevel@tonic-gate 	} else {
7930Sstevel@tonic-gate 		handle->cache_name =
7940Sstevel@tonic-gate 			(char *) malloc(strlen(ADM_CCACHE)+strlen("FILE:")+1);
7950Sstevel@tonic-gate 		if (handle->cache_name == NULL) {
7960Sstevel@tonic-gate 			code = ENOMEM;
7970Sstevel@tonic-gate 			goto error;
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 		sprintf(handle->cache_name, "FILE:%s", ADM_CCACHE);
8000Sstevel@tonic-gate 		mktemp(handle->cache_name + strlen("FILE:"));
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		if ((code = krb5_cc_resolve(handle->context,
8030Sstevel@tonic-gate 			handle->cache_name, &ccache)))
8040Sstevel@tonic-gate 			goto error;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		if ((code = krb5_cc_initialize (handle->context, ccache,
8070Sstevel@tonic-gate 					  creds.client)))
8080Sstevel@tonic-gate 			goto error;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		handle->destroy_cache = 1;
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 	handle->lhandle->cache_name = handle->cache_name;
8130Sstevel@tonic-gate 	ADMIN_LOG(LOG_ERR, dgettext(TEXT_DOMAIN,
8140Sstevel@tonic-gate 		"cache created: %s\n"), handle->cache_name);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	if ((code = krb5_timeofday(handle->context, &now)))
8170Sstevel@tonic-gate 		goto error;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * Get a ticket, use the method specified in init_type.
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	creds.times.starttime = 0; /* start timer at KDC */
8230Sstevel@tonic-gate 	creds.times.endtime = 0; /* endtime will be limited by service */
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	memset(&opt, 0, sizeof (opt));
8260Sstevel@tonic-gate 	krb5_get_init_creds_opt_init(&opt);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (creds.times.endtime) {
8290Sstevel@tonic-gate 		if (creds.times.starttime)
8300Sstevel@tonic-gate 			starttime = creds.times.starttime;
8310Sstevel@tonic-gate 		else
8320Sstevel@tonic-gate 			starttime = now;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		krb5_get_init_creds_opt_set_tkt_life(&opt,
8350Sstevel@tonic-gate 			creds.times.endtime - starttime);
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 	code = krb5_unparse_name(handle->context, creds.server, &server);
8380Sstevel@tonic-gate 	if (code)
8390Sstevel@tonic-gate 		goto error;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	if (init_type == INIT_PASS) {
8420Sstevel@tonic-gate 		code = krb5_get_init_creds_password(handle->context,
8430Sstevel@tonic-gate 			&creds, creds.client, pass, NULL,
8440Sstevel@tonic-gate 			NULL, creds.times.starttime,
8450Sstevel@tonic-gate 			server, &opt);
8460Sstevel@tonic-gate 	} else if (init_type == INIT_SKEY) {
8470Sstevel@tonic-gate 		krb5_keytab kt = NULL;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 		if (!(pass && (code = krb5_kt_resolve(handle->context,
8500Sstevel@tonic-gate 					pass, &kt)))) {
8510Sstevel@tonic-gate 			code = krb5_get_init_creds_keytab(
8520Sstevel@tonic-gate 					handle->context,
8530Sstevel@tonic-gate 					&creds, creds.client, kt,
8540Sstevel@tonic-gate 					creds.times.starttime,
8550Sstevel@tonic-gate 					server, &opt);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 			if (pass)
8580Sstevel@tonic-gate 				krb5_kt_close(handle->context, kt);
8590Sstevel@tonic-gate 		}
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	/* Improved error messages */
8630Sstevel@tonic-gate 	if (code == KRB5KRB_AP_ERR_BAD_INTEGRITY)
8640Sstevel@tonic-gate 		code = KADM5_BAD_PASSWORD;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN)
8670Sstevel@tonic-gate 		code = KADM5_SECURE_PRINC_MISSING;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	if (code != 0) {
8700Sstevel@tonic-gate 		ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN,
8710Sstevel@tonic-gate 			"failed to obtain credentials cache\n"));
8720Sstevel@tonic-gate 		goto error;
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/*
8760Sstevel@tonic-gate 	 * If we got this far, save the creds in the cache.
8770Sstevel@tonic-gate 	 */
8780Sstevel@tonic-gate 	if (ccache) {
8790Sstevel@tonic-gate 		code = krb5_cc_store_cred(handle->context, ccache, &creds);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, "obtained credentials cache\n"));
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate #ifdef ZEROPASSWD
8850Sstevel@tonic-gate 	if (pass != NULL)
8860Sstevel@tonic-gate 		memset(pass, 0, strlen(pass));
8870Sstevel@tonic-gate #endif
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if (init_type != INIT_PASS ||
89096Ssemery 	    handle->params.kpasswd_protocol == KRB5_CHGPWD_RPCSEC ||
89196Ssemery 	    cpw == FALSE) {
8920Sstevel@tonic-gate 		code = _kadm5_initialize_rpcsec_gss_handle(handle,
8930Sstevel@tonic-gate 					client_name, service_name);
8940Sstevel@tonic-gate 		if (code != 0)
8950Sstevel@tonic-gate 			goto error;
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	*server_handle = (void *) handle;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	if (init_type != INIT_CREDS)
9010Sstevel@tonic-gate 		krb5_cc_close(handle->context, ccache);
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	goto cleanup;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate error:
9060Sstevel@tonic-gate 	/*
9070Sstevel@tonic-gate 	* Note that it is illegal for this code to execute if "handle"
9080Sstevel@tonic-gate 	* has not been allocated and initialized.  I.e., don't use "goto
9090Sstevel@tonic-gate 	* error" before the block of code at the top of the function
9100Sstevel@tonic-gate 	* that allocates and initializes "handle".
9110Sstevel@tonic-gate 	*/
9120Sstevel@tonic-gate 	if (handle->cache_name)
9130Sstevel@tonic-gate 	 free(handle->cache_name);
9140Sstevel@tonic-gate 	if (handle->destroy_cache && ccache)
9150Sstevel@tonic-gate 	 krb5_cc_destroy(handle->context, ccache);
9160Sstevel@tonic-gate 	if(handle->clnt && handle->clnt->cl_auth)
9170Sstevel@tonic-gate 	  AUTH_DESTROY(handle->clnt->cl_auth);
9180Sstevel@tonic-gate 	if(handle->clnt)
9190Sstevel@tonic-gate 	  clnt_destroy(handle->clnt);
9200Sstevel@tonic-gate 	(void) kadm5_free_config_params(handle->context, &handle->params);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate cleanup:
9230Sstevel@tonic-gate 	if (server)
9240Sstevel@tonic-gate 		free(server);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
927*1692Ssemery 	 * cred's server and client pointers could have been overwritten
928*1692Ssemery 	 * by the krb5_get_init_* functions.  If the addresses are different
929*1692Ssemery 	 * before and after the calls then we must free the memory that
930*1692Ssemery 	 * was allocated before the call.
9310Sstevel@tonic-gate 	 */
932*1692Ssemery 	if (clientp && clientp != creds.client)
9330Sstevel@tonic-gate 		krb5_free_principal(handle->context, clientp);
934*1692Ssemery 
935*1692Ssemery 	if (serverp && serverp != creds.server)
9360Sstevel@tonic-gate 		krb5_free_principal(handle->context, serverp);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	krb5_free_cred_contents(handle->context, &creds);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	/*
9410Sstevel@tonic-gate 	 * Dont clean up the handle if the code is OK (code==0)
9420Sstevel@tonic-gate 	 * because it is returned to the caller in the 'server_handle'
9430Sstevel@tonic-gate 	 * ptr.
9440Sstevel@tonic-gate 	 */
9450Sstevel@tonic-gate 	if (code) {
9460Sstevel@tonic-gate 		krb5_free_context(handle->context);
9470Sstevel@tonic-gate 		free(handle->lhandle);
9480Sstevel@tonic-gate 	  free(handle);
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	return (code);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate kadm5_ret_t
9550Sstevel@tonic-gate kadm5_destroy(void *server_handle)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	krb5_ccache	    ccache = NULL;
9580Sstevel@tonic-gate 	int		    code = KADM5_OK;
9590Sstevel@tonic-gate 	kadm5_server_handle_t	handle =
9600Sstevel@tonic-gate 	  (kadm5_server_handle_t) server_handle;
9610Sstevel@tonic-gate 	OM_uint32 min_stat;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	CHECK_HANDLE(server_handle);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	if (handle->destroy_cache && handle->cache_name) {
9660Sstevel@tonic-gate 	 if ((code = krb5_cc_resolve(handle->context,
9670Sstevel@tonic-gate 				     handle->cache_name, &ccache)) == 0)
9680Sstevel@tonic-gate 	     code = krb5_cc_destroy (handle->context, ccache);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 	if (handle->cache_name)
9710Sstevel@tonic-gate 	 free(handle->cache_name);
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	if (handle->clnt && handle->clnt->cl_auth) {
9740Sstevel@tonic-gate 		/*
9750Sstevel@tonic-gate 		 * Since kadm5 doesn't use the default credentials we
9760Sstevel@tonic-gate 		 * must clean this up manually.
9770Sstevel@tonic-gate 		 */
9780Sstevel@tonic-gate 		if (handle->my_cred != GSS_C_NO_CREDENTIAL)
9790Sstevel@tonic-gate 			(void) gss_release_cred(&min_stat, &handle->my_cred);
9800Sstevel@tonic-gate 		AUTH_DESTROY(handle->clnt->cl_auth);
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 	if (handle->clnt)
9830Sstevel@tonic-gate 	  clnt_destroy(handle->clnt);
9840Sstevel@tonic-gate 	if (handle->lhandle)
9850Sstevel@tonic-gate 	    free (handle->lhandle);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	kadm5_free_config_params(handle->context, &handle->params);
9880Sstevel@tonic-gate 	krb5_free_context(handle->context);
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	handle->magic_number = 0;
9910Sstevel@tonic-gate 	free(handle);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	return (code);
9940Sstevel@tonic-gate }
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate /*ARGSUSED*/
9970Sstevel@tonic-gate kadm5_ret_t
9980Sstevel@tonic-gate kadm5_flush(void *server_handle)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	return (KADM5_OK);
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate int
10040Sstevel@tonic-gate _kadm5_check_handle(void *handle)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate 	CHECK_HANDLE(handle);
10070Sstevel@tonic-gate 	return (0);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate /*
10110Sstevel@tonic-gate  * Stub function for kadmin.  It was created to eliminate the dependency on
10120Sstevel@tonic-gate  * libkdb's ulog functions.  The srv equivalent makes the actual calls.
10130Sstevel@tonic-gate  */
10140Sstevel@tonic-gate krb5_error_code
10150Sstevel@tonic-gate kadm5_init_iprop(void *handle)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	return (0);
10180Sstevel@tonic-gate }
1019