10Sstevel@tonic-gate /* 2*4152Sps57422 * Copyright 2007 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> 452881Smp153739 #ifdef HAVE_MEMORY_H 460Sstevel@tonic-gate #include <memory.h> 472881Smp153739 #endif 480Sstevel@tonic-gate #include <string.h> 490Sstevel@tonic-gate #include <com_err.h> 500Sstevel@tonic-gate #include <sys/types.h> 510Sstevel@tonic-gate #include <sys/socket.h> 520Sstevel@tonic-gate #include <netinet/in.h> 530Sstevel@tonic-gate #include <krb5.h> 540Sstevel@tonic-gate #include <k5-int.h> /* for KRB5_ADM_DEFAULT_PORT */ 550Sstevel@tonic-gate #ifdef __STDC__ 560Sstevel@tonic-gate #include <stdlib.h> 570Sstevel@tonic-gate #endif 580Sstevel@tonic-gate #include <libintl.h> 590Sstevel@tonic-gate 602881Smp153739 #include <kadm5/admin.h> 612881Smp153739 #include <kadm5/kadm_rpc.h> 622881Smp153739 #include "client_internal.h" 632881Smp153739 640Sstevel@tonic-gate #include <syslog.h> 650Sstevel@tonic-gate #include <gssapi/gssapi.h> 660Sstevel@tonic-gate #include <gssapi_krb5.h> 670Sstevel@tonic-gate #include <gssapiP_krb5.h> 680Sstevel@tonic-gate #include <rpc/clnt.h> 692881Smp153739 700Sstevel@tonic-gate #include <iprop_hdr.h> 710Sstevel@tonic-gate #include "iprop.h" 720Sstevel@tonic-gate 730Sstevel@tonic-gate #define ADM_CCACHE "/tmp/ovsec_adm.XXXXXX" 740Sstevel@tonic-gate 752881Smp153739 static int old_auth_gssapi = 0; 760Sstevel@tonic-gate /* connection timeout to kadmind in seconds */ 770Sstevel@tonic-gate #define KADMIND_CONNECT_TIMEOUT 25 780Sstevel@tonic-gate 790Sstevel@tonic-gate int _kadm5_check_handle(); 800Sstevel@tonic-gate 810Sstevel@tonic-gate enum init_type { INIT_PASS, INIT_SKEY, INIT_CREDS }; 820Sstevel@tonic-gate 830Sstevel@tonic-gate static kadm5_ret_t _kadm5_init_any(char *client_name, 840Sstevel@tonic-gate enum init_type init_type, 850Sstevel@tonic-gate char *pass, 860Sstevel@tonic-gate krb5_ccache ccache_in, 870Sstevel@tonic-gate char *service_name, 880Sstevel@tonic-gate kadm5_config_params *params, 890Sstevel@tonic-gate krb5_ui_4 struct_version, 900Sstevel@tonic-gate krb5_ui_4 api_version, 910Sstevel@tonic-gate void **server_handle); 920Sstevel@tonic-gate 930Sstevel@tonic-gate kadm5_ret_t kadm5_init_with_creds(char *client_name, 940Sstevel@tonic-gate krb5_ccache ccache, 950Sstevel@tonic-gate char *service_name, 960Sstevel@tonic-gate kadm5_config_params *params, 970Sstevel@tonic-gate krb5_ui_4 struct_version, 980Sstevel@tonic-gate krb5_ui_4 api_version, 990Sstevel@tonic-gate void **server_handle) 1000Sstevel@tonic-gate { 1012881Smp153739 return _kadm5_init_any(client_name, INIT_CREDS, NULL, ccache, 1020Sstevel@tonic-gate service_name, params, 1030Sstevel@tonic-gate struct_version, api_version, 1040Sstevel@tonic-gate server_handle); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate kadm5_ret_t kadm5_init_with_password(char *client_name, char *pass, 1090Sstevel@tonic-gate char *service_name, 1100Sstevel@tonic-gate kadm5_config_params *params, 1110Sstevel@tonic-gate krb5_ui_4 struct_version, 1120Sstevel@tonic-gate krb5_ui_4 api_version, 1130Sstevel@tonic-gate void **server_handle) 1140Sstevel@tonic-gate { 1152881Smp153739 return _kadm5_init_any(client_name, INIT_PASS, pass, NULL, 1160Sstevel@tonic-gate service_name, params, struct_version, 1170Sstevel@tonic-gate api_version, server_handle); 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate kadm5_ret_t kadm5_init(char *client_name, char *pass, 1212881Smp153739 char *service_name, 1222881Smp153739 kadm5_config_params *params, 1232881Smp153739 krb5_ui_4 struct_version, 1242881Smp153739 krb5_ui_4 api_version, 1252881Smp153739 void **server_handle) 1260Sstevel@tonic-gate { 1272881Smp153739 return _kadm5_init_any(client_name, INIT_PASS, pass, NULL, 1280Sstevel@tonic-gate service_name, params, struct_version, 1290Sstevel@tonic-gate api_version, server_handle); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate kadm5_ret_t kadm5_init_with_skey(char *client_name, char *keytab, 1330Sstevel@tonic-gate char *service_name, 1340Sstevel@tonic-gate kadm5_config_params *params, 1350Sstevel@tonic-gate krb5_ui_4 struct_version, 1360Sstevel@tonic-gate krb5_ui_4 api_version, 1370Sstevel@tonic-gate void **server_handle) 1380Sstevel@tonic-gate { 1392881Smp153739 return _kadm5_init_any(client_name, INIT_SKEY, keytab, NULL, 1400Sstevel@tonic-gate service_name, params, struct_version, 1410Sstevel@tonic-gate api_version, server_handle); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate krb5_error_code kadm5_free_config_params(); 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate static void 1470Sstevel@tonic-gate display_status_1(m, code, type, mech) 1480Sstevel@tonic-gate char *m; 1490Sstevel@tonic-gate OM_uint32 code; 1500Sstevel@tonic-gate int type; 1510Sstevel@tonic-gate const gss_OID mech; 1520Sstevel@tonic-gate { 1530Sstevel@tonic-gate OM_uint32 maj_stat, min_stat; 1540Sstevel@tonic-gate gss_buffer_desc msg = GSS_C_EMPTY_BUFFER; 1550Sstevel@tonic-gate OM_uint32 msg_ctx; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate msg_ctx = 0; 1580Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, "%s\n", m); 1590Sstevel@tonic-gate /* LINTED */ 1600Sstevel@tonic-gate while (1) { 1610Sstevel@tonic-gate maj_stat = gss_display_status(&min_stat, code, 1620Sstevel@tonic-gate type, mech, 1630Sstevel@tonic-gate &msg_ctx, &msg); 1640Sstevel@tonic-gate if (maj_stat != GSS_S_COMPLETE) { 1650Sstevel@tonic-gate syslog(LOG_ERR, 1660Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 1670Sstevel@tonic-gate "error in gss_display_status" 1680Sstevel@tonic-gate " called from <%s>\n"), m); 1690Sstevel@tonic-gate break; 1700Sstevel@tonic-gate } else 1710Sstevel@tonic-gate syslog(LOG_ERR, dgettext(TEXT_DOMAIN, 1720Sstevel@tonic-gate "GSS-API error : %s\n"), 1730Sstevel@tonic-gate m); 1740Sstevel@tonic-gate syslog(LOG_ERR, dgettext(TEXT_DOMAIN, 1750Sstevel@tonic-gate "GSS-API error : %s\n"), 1760Sstevel@tonic-gate (char *)msg.value); 1770Sstevel@tonic-gate if (msg.length != 0) 1780Sstevel@tonic-gate (void) gss_release_buffer(&min_stat, &msg); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (!msg_ctx) 1810Sstevel@tonic-gate break; 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate /* 1860Sstevel@tonic-gate * Function: display_status 1870Sstevel@tonic-gate * 1880Sstevel@tonic-gate * Purpose: displays GSS-API messages 1890Sstevel@tonic-gate * 1900Sstevel@tonic-gate * Arguments: 1910Sstevel@tonic-gate * 1920Sstevel@tonic-gate * msg a string to be displayed with the message 1930Sstevel@tonic-gate * maj_stat the GSS-API major status code 1940Sstevel@tonic-gate * min_stat the GSS-API minor status code 1950Sstevel@tonic-gate * mech kerberos mech 1960Sstevel@tonic-gate * Effects: 1970Sstevel@tonic-gate * 1980Sstevel@tonic-gate * The GSS-API messages associated with maj_stat and min_stat are 1990Sstevel@tonic-gate * displayed on stderr, each preceeded by "GSS-API error <msg>: " and 2000Sstevel@tonic-gate * followed by a newline. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate void 2030Sstevel@tonic-gate display_status(msg, maj_stat, min_stat, mech) 2040Sstevel@tonic-gate char *msg; 2050Sstevel@tonic-gate OM_uint32 maj_stat; 2060Sstevel@tonic-gate OM_uint32 min_stat; 2070Sstevel@tonic-gate char *mech; 2080Sstevel@tonic-gate { 2090Sstevel@tonic-gate gss_OID mech_oid; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate if (!rpc_gss_mech_to_oid(mech, (rpc_gss_OID *)&mech_oid)) { 2120Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, 2130Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 2140Sstevel@tonic-gate "Invalid mechanism oid <%s>"), mech); 2150Sstevel@tonic-gate return; 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate display_status_1(msg, maj_stat, GSS_C_GSS_CODE, mech_oid); 2190Sstevel@tonic-gate display_status_1(msg, min_stat, GSS_C_MECH_CODE, mech_oid); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate /* 2230Sstevel@tonic-gate * Open an fd for the given address and connect asynchronously. Wait 2240Sstevel@tonic-gate * KADMIND_CONNECT_TIMEOUT seconds or till it succeeds. If it succeeds 2250Sstevel@tonic-gate * change fd to blocking and return it, else return -1. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate static int 2280Sstevel@tonic-gate get_connection(struct netconfig *nconf, struct netbuf netaddr) 2290Sstevel@tonic-gate { 2300Sstevel@tonic-gate struct t_info tinfo; 2310Sstevel@tonic-gate struct t_call sndcall; 2320Sstevel@tonic-gate struct t_call *rcvcall = NULL; 2330Sstevel@tonic-gate int connect_time; 2340Sstevel@tonic-gate int flags; 2350Sstevel@tonic-gate int fd; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate (void) memset(&tinfo, 0, sizeof (tinfo)); 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* we'l open with O_NONBLOCK and avoid an fcntl */ 2400Sstevel@tonic-gate fd = t_open(nconf->nc_device, O_RDWR | O_NONBLOCK, &tinfo); 2410Sstevel@tonic-gate if (fd == -1) { 2420Sstevel@tonic-gate return (-1); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate if (t_bind(fd, (struct t_bind *)NULL, (struct t_bind *)NULL) == -1) { 2460Sstevel@tonic-gate (void) close(fd); 2470Sstevel@tonic-gate return (-1); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* we can't connect unless fd is in IDLE state */ 2510Sstevel@tonic-gate if (t_getstate(fd) != T_IDLE) { 2520Sstevel@tonic-gate (void) close(fd); 2530Sstevel@tonic-gate return (-1); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* setup connect parameters */ 2570Sstevel@tonic-gate netaddr.len = netaddr.maxlen = __rpc_get_a_size(tinfo.addr); 2580Sstevel@tonic-gate sndcall.addr = netaddr; 2590Sstevel@tonic-gate sndcall.opt.len = sndcall.udata.len = 0; 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate /* we wait for KADMIND_CONNECT_TIMEOUT seconds from now */ 2620Sstevel@tonic-gate connect_time = time(NULL) + KADMIND_CONNECT_TIMEOUT; 2630Sstevel@tonic-gate if (t_connect(fd, &sndcall, rcvcall) != 0) { 2640Sstevel@tonic-gate if (t_errno != TNODATA) { 2650Sstevel@tonic-gate (void) close(fd); 2660Sstevel@tonic-gate return (-1); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate /* loop till success or timeout */ 2710Sstevel@tonic-gate for (;;) { 2720Sstevel@tonic-gate if (t_rcvconnect(fd, rcvcall) == 0) 2730Sstevel@tonic-gate break; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate if (t_errno != TNODATA || time(NULL) > connect_time) { 2760Sstevel@tonic-gate /* we have either timed out or caught an error */ 2770Sstevel@tonic-gate (void) close(fd); 2780Sstevel@tonic-gate if (rcvcall != NULL) 2790Sstevel@tonic-gate t_free((char *)rcvcall, T_CALL); 2800Sstevel@tonic-gate return (-1); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate sleep(1); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* make the fd blocking (synchronous) */ 2860Sstevel@tonic-gate flags = fcntl(fd, F_GETFL, 0); 2870Sstevel@tonic-gate (void) fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); 2880Sstevel@tonic-gate if (rcvcall != NULL) 2890Sstevel@tonic-gate t_free((char *)rcvcall, T_CALL); 2900Sstevel@tonic-gate return (fd); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate /* 2940Sstevel@tonic-gate * Open an RPCSEC_GSS connection and 2950Sstevel@tonic-gate * get a client handle to use for future RPCSEC calls. 2960Sstevel@tonic-gate * 2970Sstevel@tonic-gate * This function is only used when changing passwords and 2980Sstevel@tonic-gate * the kpasswd_protocol is RPCSEC_GSS 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate static int 3010Sstevel@tonic-gate _kadm5_initialize_rpcsec_gss_handle(kadm5_server_handle_t handle, 3020Sstevel@tonic-gate char *client_name, 3030Sstevel@tonic-gate char *service_name) 3040Sstevel@tonic-gate { 3050Sstevel@tonic-gate struct netbuf netaddr; 3060Sstevel@tonic-gate struct hostent *hp; 3070Sstevel@tonic-gate int fd; 3080Sstevel@tonic-gate struct sockaddr_in addr; 3090Sstevel@tonic-gate struct sockaddr_in *sin; 3100Sstevel@tonic-gate struct netconfig *nconf; 3110Sstevel@tonic-gate int code = 0; 3120Sstevel@tonic-gate generic_ret *r; 3130Sstevel@tonic-gate char *ccname_orig; 3140Sstevel@tonic-gate char *iprop_svc; 3150Sstevel@tonic-gate boolean_t iprop_enable = B_FALSE; 3160Sstevel@tonic-gate char mech[] = "kerberos_v5"; 3170Sstevel@tonic-gate gss_OID mech_oid; 3180Sstevel@tonic-gate gss_OID_set_desc oid_set; 3190Sstevel@tonic-gate gss_name_t gss_client; 3200Sstevel@tonic-gate gss_buffer_desc input_name; 3210Sstevel@tonic-gate gss_cred_id_t gss_client_creds = GSS_C_NO_CREDENTIAL; 3220Sstevel@tonic-gate rpc_gss_options_req_t options_req; 3230Sstevel@tonic-gate rpc_gss_options_ret_t options_ret; 3240Sstevel@tonic-gate rpc_gss_service_t service = rpc_gss_svc_privacy; 3250Sstevel@tonic-gate OM_uint32 gssstat, minor_stat; 3260Sstevel@tonic-gate void *handlep; 3270Sstevel@tonic-gate enum clnt_stat rpc_err_code; 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate hp = gethostbyname(handle->params.admin_server); 3300Sstevel@tonic-gate if (hp == (struct hostent *)NULL) { 3310Sstevel@tonic-gate code = KADM5_BAD_SERVER_NAME; 3320Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 3330Sstevel@tonic-gate "bad server name\n")); 3340Sstevel@tonic-gate goto cleanup; 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate memset(&addr, 0, sizeof (addr)); 3380Sstevel@tonic-gate addr.sin_family = hp->h_addrtype; 3390Sstevel@tonic-gate (void) memcpy((char *)&addr.sin_addr, (char *)hp->h_addr, 3400Sstevel@tonic-gate sizeof (addr.sin_addr)); 3410Sstevel@tonic-gate addr.sin_port = htons((ushort_t)handle->params.kadmind_port); 3420Sstevel@tonic-gate sin = &addr; 3430Sstevel@tonic-gate #ifdef DEBUG 3440Sstevel@tonic-gate printf("kadmin_port %d\n", handle->params.kadmind_port); 3450Sstevel@tonic-gate printf("addr: sin_port: %d, sin_family: %d, sin_zero %s\n", 3460Sstevel@tonic-gate addr.sin_port, addr.sin_family, addr.sin_zero); 3470Sstevel@tonic-gate printf("sin_addr %d:%d\n", addr.sin_addr.S_un.S_un_w.s_w1, 3480Sstevel@tonic-gate addr.sin_addr.S_un.S_un_w.s_w2); 3490Sstevel@tonic-gate #endif 3500Sstevel@tonic-gate if ((handlep = setnetconfig()) == (void *) NULL) { 3510Sstevel@tonic-gate (void) syslog(LOG_ERR, 3520Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 3530Sstevel@tonic-gate "cannot get any transport information")); 3540Sstevel@tonic-gate goto error; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate while (nconf = getnetconfig(handlep)) { 3580Sstevel@tonic-gate if ((nconf->nc_semantics == NC_TPI_COTS_ORD) && 3590Sstevel@tonic-gate (strcmp(nconf->nc_protofmly, NC_INET) == 0) && 3600Sstevel@tonic-gate (strcmp(nconf->nc_proto, NC_TCP) == 0)) 3610Sstevel@tonic-gate break; 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate if (nconf == (struct netconfig *)NULL) 3650Sstevel@tonic-gate goto error; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate /* Transform addr to netbuf */ 3680Sstevel@tonic-gate (void) memset(&netaddr, 0, sizeof (netaddr)); 3690Sstevel@tonic-gate netaddr.buf = (char *)sin; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* get an fd connected to the given address */ 3720Sstevel@tonic-gate fd = get_connection(nconf, netaddr); 3730Sstevel@tonic-gate if (fd == -1) { 3740Sstevel@tonic-gate syslog(LOG_ERR, dgettext(TEXT_DOMAIN, 3750Sstevel@tonic-gate "unable to open connection to ADMIN server " 3760Sstevel@tonic-gate "(t_error %i)"), t_errno); 3770Sstevel@tonic-gate code = KADM5_RPC_ERROR; 3780Sstevel@tonic-gate goto error; 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate #ifdef DEBUG 3820Sstevel@tonic-gate printf("fd: %d, KADM: %d, KADMVERS %d\n", fd, KADM, KADMVERS); 3830Sstevel@tonic-gate printf("nconf: nc_netid: %s, nc_semantics: %d, nc_flag: %d, " 3840Sstevel@tonic-gate "nc_protofmly: %s\n", 3850Sstevel@tonic-gate nconf->nc_netid, nconf->nc_semantics, nconf->nc_flag, 3860Sstevel@tonic-gate nconf->nc_protofmly); 3870Sstevel@tonic-gate printf("nc_proto: %s, nc_device: %s, nc_nlookups: %d, nc_used: %d\n", 3880Sstevel@tonic-gate nconf->nc_proto, nconf->nc_device, nconf->nc_nlookups, 3890Sstevel@tonic-gate nconf->nc_unused); 3900Sstevel@tonic-gate printf("netaddr: maxlen %d, buf: %s, len: %d\n", netaddr.maxlen, 3910Sstevel@tonic-gate netaddr.buf, netaddr.len); 3920Sstevel@tonic-gate #endif 3930Sstevel@tonic-gate /* 3940Sstevel@tonic-gate * Tell clnt_tli_create that given fd is already connected 3950Sstevel@tonic-gate * 3960Sstevel@tonic-gate * If the service_name and client_name are iprop-centric, 3970Sstevel@tonic-gate * we need to clnt_tli_create to the appropriate RPC prog 3980Sstevel@tonic-gate */ 3990Sstevel@tonic-gate iprop_svc = strdup(KIPROP_SVC_NAME); 4000Sstevel@tonic-gate if (iprop_svc == NULL) 4010Sstevel@tonic-gate return (ENOMEM); 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate if ((strstr(service_name, iprop_svc) != NULL) && 4040Sstevel@tonic-gate (strstr(client_name, iprop_svc) != NULL)) { 4050Sstevel@tonic-gate iprop_enable = B_TRUE; 4060Sstevel@tonic-gate handle->clnt = clnt_tli_create(fd, nconf, NULL, 4070Sstevel@tonic-gate KRB5_IPROP_PROG, KRB5_IPROP_VERS, 0, 0); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate else 4100Sstevel@tonic-gate handle->clnt = clnt_tli_create(fd, nconf, NULL, 4110Sstevel@tonic-gate KADM, KADMVERS, 0, 0); 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate if (iprop_svc) 4140Sstevel@tonic-gate free(iprop_svc); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate if (handle->clnt == NULL) { 4170Sstevel@tonic-gate syslog(LOG_ERR, dgettext(TEXT_DOMAIN, 4180Sstevel@tonic-gate "clnt_tli_create failed\n")); 4190Sstevel@tonic-gate code = KADM5_RPC_ERROR; 4200Sstevel@tonic-gate (void) close(fd); 4210Sstevel@tonic-gate goto error; 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * The rpc-handle was created on an fd opened and connected 4250Sstevel@tonic-gate * by us, so we have to explicitly tell rpc to close it. 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate if (clnt_control(handle->clnt, CLSET_FD_CLOSE, NULL) != TRUE) { 4280Sstevel@tonic-gate clnt_pcreateerror("ERROR:"); 4290Sstevel@tonic-gate syslog(LOG_ERR, dgettext(TEXT_DOMAIN, 4300Sstevel@tonic-gate "clnt_control failed to set CLSET_FD_CLOSE")); 4310Sstevel@tonic-gate code = KADM5_RPC_ERROR; 4320Sstevel@tonic-gate (void) close(fd); 4330Sstevel@tonic-gate goto error; 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate handle->lhandle->clnt = handle->clnt; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* now that handle->clnt is set, we can check the handle */ 4390Sstevel@tonic-gate if (code = _kadm5_check_handle((void *) handle)) 4400Sstevel@tonic-gate goto error; 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate /* 4430Sstevel@tonic-gate * The RPC connection is open; establish the GSS-API 4440Sstevel@tonic-gate * authentication context. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 4470Sstevel@tonic-gate "have an rpc connection open\n")); 4480Sstevel@tonic-gate /* use the kadm5 cache */ 4490Sstevel@tonic-gate ccname_orig = getenv("KRB5CCNAME"); 4500Sstevel@tonic-gate if (ccname_orig) 4510Sstevel@tonic-gate ccname_orig = strdup(ccname_orig); 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate (void) krb5_setenv("KRB5CCNAME", handle->cache_name, 1); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, 4560Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 4570Sstevel@tonic-gate "current credential cache: %s"), handle->cache_name); 4580Sstevel@tonic-gate input_name.value = client_name; 4590Sstevel@tonic-gate input_name.length = strlen((char *)input_name.value) + 1; 4600Sstevel@tonic-gate gssstat = gss_import_name(&minor_stat, &input_name, 4610Sstevel@tonic-gate (gss_OID)gss_nt_krb5_name, &gss_client); 4620Sstevel@tonic-gate if (gssstat != GSS_S_COMPLETE) { 4630Sstevel@tonic-gate code = KADM5_GSS_ERROR; 4640Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, 4650Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 4660Sstevel@tonic-gate "gss_import_name failed for client name\n")); 4670Sstevel@tonic-gate goto error; 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate if (!rpc_gss_mech_to_oid(mech, (rpc_gss_OID *)&mech_oid)) { 4710Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, 4720Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 4730Sstevel@tonic-gate "Invalid mechanism oid <%s>"), mech); 4740Sstevel@tonic-gate goto error; 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate oid_set.count = 1; 4780Sstevel@tonic-gate oid_set.elements = mech_oid; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate gssstat = gss_acquire_cred(&minor_stat, gss_client, 0, 4810Sstevel@tonic-gate &oid_set, GSS_C_INITIATE, 4820Sstevel@tonic-gate &gss_client_creds, NULL, NULL); 4830Sstevel@tonic-gate (void) gss_release_name(&minor_stat, &gss_client); 4840Sstevel@tonic-gate if (gssstat != GSS_S_COMPLETE) { 4850Sstevel@tonic-gate code = KADM5_GSS_ERROR; 4860Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, 4870Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 4880Sstevel@tonic-gate "could not acquire credentials, " 4890Sstevel@tonic-gate "major error code: %d\n"), gssstat); 4900Sstevel@tonic-gate goto error; 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate handle->my_cred = gss_client_creds; 4930Sstevel@tonic-gate options_req.my_cred = gss_client_creds; 4940Sstevel@tonic-gate options_req.req_flags = GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG; 4950Sstevel@tonic-gate options_req.time_req = 0; 4960Sstevel@tonic-gate options_req.input_channel_bindings = NULL; 4970Sstevel@tonic-gate #ifndef INIT_TEST 4980Sstevel@tonic-gate handle->clnt->cl_auth = rpc_gss_seccreate(handle->clnt, 4990Sstevel@tonic-gate service_name, 5000Sstevel@tonic-gate mech, 5010Sstevel@tonic-gate service, 5020Sstevel@tonic-gate NULL, 5030Sstevel@tonic-gate &options_req, 5040Sstevel@tonic-gate &options_ret); 5050Sstevel@tonic-gate #endif /* ! INIT_TEST */ 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate if (ccname_orig) { 5080Sstevel@tonic-gate (void) krb5_setenv("KRB5CCNAME", ccname_orig, 1); 5090Sstevel@tonic-gate free(ccname_orig); 5100Sstevel@tonic-gate } else 5110Sstevel@tonic-gate (void) krb5_unsetenv("KRB5CCNAME"); 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate if (handle->clnt->cl_auth == NULL) { 5140Sstevel@tonic-gate code = KADM5_GSS_ERROR; 5150Sstevel@tonic-gate display_status(dgettext(TEXT_DOMAIN, 5160Sstevel@tonic-gate "rpc_gss_seccreate failed\n"), 5170Sstevel@tonic-gate options_ret.major_status, 5180Sstevel@tonic-gate options_ret.minor_status, 5190Sstevel@tonic-gate mech); 5200Sstevel@tonic-gate goto error; 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* 5240Sstevel@tonic-gate * Bypass the remainder of the code and return straightaway 5250Sstevel@tonic-gate * if the gss service requested is kiprop 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate if (iprop_enable == B_TRUE) { 5280Sstevel@tonic-gate code = 0; 5290Sstevel@tonic-gate goto cleanup; 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate r = init_1(&handle->api_version, handle->clnt, &rpc_err_code); 5330Sstevel@tonic-gate if (r == NULL) { 5340Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 5350Sstevel@tonic-gate "error during admin api initialization\n")); 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate if (rpc_err_code == RPC_CANTENCODEARGS) { 5380Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 5390Sstevel@tonic-gate "encryption needed to encode RPC data may not be " 5400Sstevel@tonic-gate "installed/configured on this system")); 5410Sstevel@tonic-gate code = KADM5_RPC_ERROR_CANTENCODEARGS; 5420Sstevel@tonic-gate } else if (rpc_err_code == RPC_CANTDECODEARGS) { 5430Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 5440Sstevel@tonic-gate "encryption needed to decode RPC data may not be " 5450Sstevel@tonic-gate "installed/configured on the server")); 5460Sstevel@tonic-gate code = KADM5_RPC_ERROR_CANTDECODEARGS; 5470Sstevel@tonic-gate } else 5480Sstevel@tonic-gate code = KADM5_RPC_ERROR; 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate goto error; 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate if (r->code) { 5540Sstevel@tonic-gate code = r->code; 5550Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, 5560Sstevel@tonic-gate dgettext(TEXT_DOMAIN, 5570Sstevel@tonic-gate "error during admin api initialization: %d\n"), 5580Sstevel@tonic-gate r->code); 5590Sstevel@tonic-gate goto error; 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate error: 5620Sstevel@tonic-gate cleanup: 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate if (handlep != (void *) NULL) 5650Sstevel@tonic-gate (void) endnetconfig(handlep); 5660Sstevel@tonic-gate /* 5670Sstevel@tonic-gate * gss_client_creds is freed only when there is an error condition, 5680Sstevel@tonic-gate * given that rpc_gss_seccreate() will assign the cred pointer to the 5690Sstevel@tonic-gate * my_cred member in the auth handle's private data structure. 5700Sstevel@tonic-gate */ 5710Sstevel@tonic-gate if (code && (gss_client_creds != GSS_C_NO_CREDENTIAL)) 5720Sstevel@tonic-gate (void) gss_release_cred(&minor_stat, &gss_client_creds); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate return (code); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate static kadm5_ret_t _kadm5_init_any(char *client_name, 5780Sstevel@tonic-gate enum init_type init_type, 5790Sstevel@tonic-gate char *pass, 5800Sstevel@tonic-gate krb5_ccache ccache_in, 5810Sstevel@tonic-gate char *service_name, 5820Sstevel@tonic-gate kadm5_config_params *params_in, 5830Sstevel@tonic-gate krb5_ui_4 struct_version, 5840Sstevel@tonic-gate krb5_ui_4 api_version, 5850Sstevel@tonic-gate void **server_handle) 5860Sstevel@tonic-gate { 5872881Smp153739 int i; 5882881Smp153739 krb5_creds creds; 5892881Smp153739 krb5_ccache ccache = NULL; 5902881Smp153739 krb5_timestamp now; 5912881Smp153739 OM_uint32 gssstat, minor_stat; 5922881Smp153739 kadm5_server_handle_t handle; 5932881Smp153739 kadm5_config_params params_local; 5942881Smp153739 int code = 0; 5952881Smp153739 krb5_get_init_creds_opt opt; 5962881Smp153739 gss_buffer_desc input_name; 5972881Smp153739 krb5_error_code kret; 5982881Smp153739 krb5_int32 starttime; 5992881Smp153739 char *server = NULL; 6002881Smp153739 krb5_principal serverp = NULL, clientp = NULL; 6012881Smp153739 bool_t cpw = FALSE; 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 6040Sstevel@tonic-gate "entering kadm5_init_any\n")); 6052881Smp153739 if (! server_handle) { 6062881Smp153739 return EINVAL; 6072881Smp153739 } 6080Sstevel@tonic-gate 6092881Smp153739 if (! (handle = malloc(sizeof(*handle)))) { 6102881Smp153739 return ENOMEM; 6112881Smp153739 } 6122881Smp153739 if (! (handle->lhandle = malloc(sizeof(*handle)))) { 6132881Smp153739 free(handle); 6142881Smp153739 return ENOMEM; 6152881Smp153739 } 6160Sstevel@tonic-gate 6172881Smp153739 handle->magic_number = KADM5_SERVER_HANDLE_MAGIC; 6182881Smp153739 handle->struct_version = struct_version; 6192881Smp153739 handle->api_version = api_version; 6202881Smp153739 handle->clnt = 0; 6212881Smp153739 handle->cache_name = 0; 6222881Smp153739 handle->destroy_cache = 0; 6232881Smp153739 *handle->lhandle = *handle; 6242881Smp153739 handle->lhandle->api_version = KADM5_API_VERSION_2; 6252881Smp153739 handle->lhandle->struct_version = KADM5_STRUCT_VERSION; 6262881Smp153739 handle->lhandle->lhandle = handle->lhandle; 6270Sstevel@tonic-gate 6282881Smp153739 kret = krb5_init_context(&handle->context); 6290Sstevel@tonic-gate if (kret) { 6300Sstevel@tonic-gate free(handle->lhandle); 6310Sstevel@tonic-gate free(handle); 6320Sstevel@tonic-gate return (kret); 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate 6352881Smp153739 if(service_name == NULL || client_name == NULL) { 6362881Smp153739 krb5_free_context(handle->context); 6372881Smp153739 free(handle->lhandle); 6382881Smp153739 free(handle); 6392881Smp153739 return EINVAL; 6402881Smp153739 } 6412881Smp153739 memset((char *) &creds, 0, sizeof(creds)); 6420Sstevel@tonic-gate 6432881Smp153739 /* 6442881Smp153739 * Verify the version numbers before proceeding; we can't use 6452881Smp153739 * CHECK_HANDLE because not all fields are set yet. 6462881Smp153739 */ 6472881Smp153739 GENERIC_CHECK_HANDLE(handle, KADM5_OLD_LIB_API_VERSION, 6480Sstevel@tonic-gate KADM5_NEW_LIB_API_VERSION); 6492881Smp153739 6502881Smp153739 /* 6512881Smp153739 * Acquire relevant profile entries. In version 2, merge values 6522881Smp153739 * in params_in with values from profile, based on 6532881Smp153739 * params_in->mask. 6542881Smp153739 * 6552881Smp153739 * In version 1, we've given a realm (which may be NULL) instead 6562881Smp153739 * of params_in. So use that realm, make params_in contain an 6572881Smp153739 * empty mask, and behave like version 2. 6582881Smp153739 */ 6592881Smp153739 memset((char *) ¶ms_local, 0, sizeof(params_local)); 6602881Smp153739 if (api_version == KADM5_API_VERSION_1) { 6612881Smp153739 if (params_in) 6622881Smp153739 params_local.mask = KADM5_CONFIG_REALM; 6632881Smp153739 params_in = ¶ms_local; 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate #define ILLEGAL_PARAMS ( \ 6672881Smp153739 KADM5_CONFIG_ACL_FILE | KADM5_CONFIG_ADB_LOCKFILE | \ 6682881Smp153739 KADM5_CONFIG_DBNAME | KADM5_CONFIG_ADBNAME | \ 6692881Smp153739 KADM5_CONFIG_DICT_FILE | KADM5_CONFIG_ADMIN_KEYTAB | \ 6702881Smp153739 KADM5_CONFIG_STASH_FILE | KADM5_CONFIG_MKEY_NAME | \ 6712881Smp153739 KADM5_CONFIG_ENCTYPE | KADM5_CONFIG_MAX_LIFE | \ 6722881Smp153739 KADM5_CONFIG_MAX_RLIFE | KADM5_CONFIG_EXPIRATION | \ 6732881Smp153739 KADM5_CONFIG_FLAGS | KADM5_CONFIG_ENCTYPES | \ 6742881Smp153739 KADM5_CONFIG_MKEY_FROM_KBD) 6750Sstevel@tonic-gate 6762881Smp153739 if (params_in && params_in->mask & ILLEGAL_PARAMS) { 6770Sstevel@tonic-gate krb5_free_context(handle->context); 6780Sstevel@tonic-gate free(handle->lhandle); 6792881Smp153739 free(handle); 6800Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, dgettext(TEXT_DOMAIN, 6810Sstevel@tonic-gate "bad client parameters, returning %d"), 6820Sstevel@tonic-gate KADM5_BAD_CLIENT_PARAMS); 6832881Smp153739 return KADM5_BAD_CLIENT_PARAMS; 6842881Smp153739 } 6850Sstevel@tonic-gate 6862881Smp153739 if ((code = kadm5_get_config_params(handle->context, 6870Sstevel@tonic-gate DEFAULT_PROFILE_PATH, 6880Sstevel@tonic-gate "KRB5_CONFIG", 6890Sstevel@tonic-gate params_in, 6900Sstevel@tonic-gate &handle->params))) { 6912881Smp153739 krb5_free_context(handle->context); 6922881Smp153739 free(handle->lhandle); 6932881Smp153739 free(handle); 6940Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, dgettext(TEXT_DOMAIN, 6950Sstevel@tonic-gate "failed to get config_params, return: %d\n"), code); 6962881Smp153739 return(code); 6972881Smp153739 } 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate #define REQUIRED_PARAMS (KADM5_CONFIG_REALM | \ 7000Sstevel@tonic-gate KADM5_CONFIG_ADMIN_SERVER | \ 7010Sstevel@tonic-gate KADM5_CONFIG_KADMIND_PORT) 7020Sstevel@tonic-gate 7032881Smp153739 if ((handle->params.mask & REQUIRED_PARAMS) != REQUIRED_PARAMS) { 7040Sstevel@tonic-gate (void) kadm5_free_config_params(handle->context, 7050Sstevel@tonic-gate &handle->params); 7062881Smp153739 krb5_free_context(handle->context); 7070Sstevel@tonic-gate free(handle->lhandle); 7082881Smp153739 free(handle); 7090Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 7100Sstevel@tonic-gate "missing config parameters\n")); 7112881Smp153739 return KADM5_MISSING_KRB5_CONF_PARAMS; 7122881Smp153739 } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * Acquire a service ticket for service_name@realm in the name of 7160Sstevel@tonic-gate * client_name, using password pass (which could be NULL), and 7170Sstevel@tonic-gate * create a ccache to store them in. If INIT_CREDS, use the 7180Sstevel@tonic-gate * ccache we were provided instead. 7190Sstevel@tonic-gate */ 7200Sstevel@tonic-gate if ((code = krb5_parse_name(handle->context, client_name, 7210Sstevel@tonic-gate &creds.client))) { 7220Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 7230Sstevel@tonic-gate "could not parse client name\n")); 7240Sstevel@tonic-gate goto error; 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate clientp = creds.client; 7270Sstevel@tonic-gate 72896Ssemery if (strncmp(service_name, KADM5_CHANGEPW_HOST_SERVICE, 72996Ssemery strlen(KADM5_CHANGEPW_HOST_SERVICE)) == 0) 73096Ssemery cpw = TRUE; 73196Ssemery 7320Sstevel@tonic-gate if (init_type == INIT_PASS && 73396Ssemery handle->params.kpasswd_protocol == KRB5_CHGPWD_CHANGEPW_V2 && 73496Ssemery cpw == TRUE) { 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * The 'service_name' is constructed by the caller 7370Sstevel@tonic-gate * but its done before the parameter which determines 7380Sstevel@tonic-gate * the kpasswd_protocol is found. The servers that 7390Sstevel@tonic-gate * support the SET/CHANGE password protocol expect 7400Sstevel@tonic-gate * a slightly different service principal than 7410Sstevel@tonic-gate * the normal SEAM kadmind so construct the correct 7420Sstevel@tonic-gate * name here and then forget it. 7430Sstevel@tonic-gate */ 7440Sstevel@tonic-gate char *newsvcname = NULL; 7450Sstevel@tonic-gate newsvcname = malloc(strlen(KADM5_CHANGEPW_SERVICE) + 7460Sstevel@tonic-gate strlen(handle->params.realm) + 2); 7470Sstevel@tonic-gate if (newsvcname == NULL) { 748*4152Sps57422 ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 749*4152Sps57422 "could not malloc\n")); 750*4152Sps57422 code = ENOMEM; 751*4152Sps57422 goto error; 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate sprintf(newsvcname, "%s@%s", KADM5_CHANGEPW_SERVICE, 7540Sstevel@tonic-gate handle->params.realm); 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate if ((code = krb5_parse_name(handle->context, newsvcname, 7570Sstevel@tonic-gate &creds.server))) { 7580Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 7590Sstevel@tonic-gate "could not parse server " 7600Sstevel@tonic-gate "name\n")); 7610Sstevel@tonic-gate free(newsvcname); 7620Sstevel@tonic-gate goto error; 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate free(newsvcname); 7650Sstevel@tonic-gate } else { 7660Sstevel@tonic-gate input_name.value = service_name; 7670Sstevel@tonic-gate input_name.length = strlen((char *)input_name.value) + 1; 7680Sstevel@tonic-gate gssstat = krb5_gss_import_name(handle->context, 7690Sstevel@tonic-gate &minor_stat, 7700Sstevel@tonic-gate &input_name, 7710Sstevel@tonic-gate (gss_OID)GSS_C_NT_HOSTBASED_SERVICE, 7720Sstevel@tonic-gate (gss_name_t *)&creds.server); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate if (gssstat != GSS_S_COMPLETE) { 7750Sstevel@tonic-gate code = KADM5_GSS_ERROR; 7760Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 7770Sstevel@tonic-gate "gss_import_name failed for client name\n")); 7780Sstevel@tonic-gate goto error; 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate } 7810Sstevel@tonic-gate serverp = creds.server; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate /* XXX temporarily fix a bug in krb5_cc_get_type */ 7840Sstevel@tonic-gate #undef krb5_cc_get_type 7850Sstevel@tonic-gate #define krb5_cc_get_type(context, cache) ((cache)->ops->prefix) 7862881Smp153739 7870Sstevel@tonic-gate 7882881Smp153739 if (init_type == INIT_CREDS) { 7892881Smp153739 ccache = ccache_in; 7902881Smp153739 handle->cache_name = (char *) 7912881Smp153739 malloc(strlen(krb5_cc_get_type(handle->context, ccache)) + 7922881Smp153739 strlen(krb5_cc_get_name(handle->context, ccache)) + 2); 7932881Smp153739 if (handle->cache_name == NULL) { 7942881Smp153739 code = ENOMEM; 7952881Smp153739 goto error; 7962881Smp153739 } 7972881Smp153739 sprintf(handle->cache_name, "%s:%s", 7982881Smp153739 krb5_cc_get_type(handle->context, ccache), 7992881Smp153739 krb5_cc_get_name(handle->context, ccache)); 8002881Smp153739 } else { 8012881Smp153739 #if 0 8022881Smp153739 handle->cache_name = 8032881Smp153739 (char *) malloc(strlen(ADM_CCACHE)+strlen("FILE:")+1); 8042881Smp153739 if (handle->cache_name == NULL) { 8052881Smp153739 code = ENOMEM; 8062881Smp153739 goto error; 8072881Smp153739 } 8082881Smp153739 sprintf(handle->cache_name, "FILE:%s", ADM_CCACHE); 8092881Smp153739 mktemp(handle->cache_name + strlen("FILE:")); 8102881Smp153739 #endif 8112881Smp153739 { 8122881Smp153739 static int counter = 0; 8132881Smp153739 handle->cache_name = malloc(sizeof("MEMORY:kadm5_") 8142881Smp153739 + 3*sizeof(counter)); 8152881Smp153739 sprintf(handle->cache_name, "MEMORY:kadm5_%u", counter++); 8162881Smp153739 } 8172881Smp153739 8182881Smp153739 if ((code = krb5_cc_resolve(handle->context, handle->cache_name, 8192881Smp153739 &ccache))) 8202881Smp153739 goto error; 8212881Smp153739 8222881Smp153739 if ((code = krb5_cc_initialize (handle->context, ccache, 8232881Smp153739 creds.client))) 8242881Smp153739 goto error; 8250Sstevel@tonic-gate 8262881Smp153739 handle->destroy_cache = 1; 8272881Smp153739 } 8282881Smp153739 handle->lhandle->cache_name = handle->cache_name; 8290Sstevel@tonic-gate ADMIN_LOG(LOG_ERR, dgettext(TEXT_DOMAIN, 8300Sstevel@tonic-gate "cache created: %s\n"), handle->cache_name); 8312881Smp153739 8322881Smp153739 if ((code = krb5_timeofday(handle->context, &now))) 8332881Smp153739 goto error; 8340Sstevel@tonic-gate 8352881Smp153739 /* 8362881Smp153739 * Get a ticket, use the method specified in init_type. 8372881Smp153739 */ 8382881Smp153739 8392881Smp153739 creds.times.starttime = 0; /* start timer at KDC */ 8402881Smp153739 creds.times.endtime = 0; /* endtime will be limited by service */ 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate memset(&opt, 0, sizeof (opt)); 8430Sstevel@tonic-gate krb5_get_init_creds_opt_init(&opt); 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate if (creds.times.endtime) { 8460Sstevel@tonic-gate if (creds.times.starttime) 8470Sstevel@tonic-gate starttime = creds.times.starttime; 8480Sstevel@tonic-gate else 8490Sstevel@tonic-gate starttime = now; 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate krb5_get_init_creds_opt_set_tkt_life(&opt, 8520Sstevel@tonic-gate creds.times.endtime - starttime); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate code = krb5_unparse_name(handle->context, creds.server, &server); 8550Sstevel@tonic-gate if (code) 8560Sstevel@tonic-gate goto error; 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate if (init_type == INIT_PASS) { 8590Sstevel@tonic-gate code = krb5_get_init_creds_password(handle->context, 8600Sstevel@tonic-gate &creds, creds.client, pass, NULL, 8610Sstevel@tonic-gate NULL, creds.times.starttime, 8620Sstevel@tonic-gate server, &opt); 8630Sstevel@tonic-gate } else if (init_type == INIT_SKEY) { 8640Sstevel@tonic-gate krb5_keytab kt = NULL; 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate if (!(pass && (code = krb5_kt_resolve(handle->context, 8670Sstevel@tonic-gate pass, &kt)))) { 8680Sstevel@tonic-gate code = krb5_get_init_creds_keytab( 8690Sstevel@tonic-gate handle->context, 8700Sstevel@tonic-gate &creds, creds.client, kt, 8710Sstevel@tonic-gate creds.times.starttime, 8720Sstevel@tonic-gate server, &opt); 8730Sstevel@tonic-gate 8742881Smp153739 if (pass) krb5_kt_close(handle->context, kt); 8752881Smp153739 } 8762881Smp153739 } 8770Sstevel@tonic-gate 8782881Smp153739 /* Improved error messages */ 8792881Smp153739 if (code == KRB5KRB_AP_ERR_BAD_INTEGRITY) code = KADM5_BAD_PASSWORD; 8802881Smp153739 if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) 8812881Smp153739 code = KADM5_SECURE_PRINC_MISSING; 8820Sstevel@tonic-gate 8832881Smp153739 if (code != 0) { 8840Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, 8850Sstevel@tonic-gate "failed to obtain credentials cache\n")); 8860Sstevel@tonic-gate goto error; 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate /* 8900Sstevel@tonic-gate * If we got this far, save the creds in the cache. 8910Sstevel@tonic-gate */ 8920Sstevel@tonic-gate if (ccache) { 8930Sstevel@tonic-gate code = krb5_cc_store_cred(handle->context, ccache, &creds); 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate ADMIN_LOGO(LOG_ERR, dgettext(TEXT_DOMAIN, "obtained credentials cache\n")); 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate #ifdef ZEROPASSWD 8992881Smp153739 if (pass != NULL) 9002881Smp153739 memset(pass, 0, strlen(pass)); 9010Sstevel@tonic-gate #endif 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate if (init_type != INIT_PASS || 90496Ssemery handle->params.kpasswd_protocol == KRB5_CHGPWD_RPCSEC || 90596Ssemery cpw == FALSE) { 9060Sstevel@tonic-gate code = _kadm5_initialize_rpcsec_gss_handle(handle, 9070Sstevel@tonic-gate client_name, service_name); 9080Sstevel@tonic-gate if (code != 0) 9090Sstevel@tonic-gate goto error; 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate *server_handle = (void *) handle; 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate if (init_type != INIT_CREDS) 9150Sstevel@tonic-gate krb5_cc_close(handle->context, ccache); 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate goto cleanup; 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate error: 9202881Smp153739 /* 9212881Smp153739 * Note that it is illegal for this code to execute if "handle" 9222881Smp153739 * has not been allocated and initialized. I.e., don't use "goto 9232881Smp153739 * error" before the block of code at the top of the function 9242881Smp153739 * that allocates and initializes "handle". 9252881Smp153739 */ 9262881Smp153739 if (handle->cache_name) 9270Sstevel@tonic-gate free(handle->cache_name); 9282881Smp153739 if (handle->destroy_cache && ccache) 9290Sstevel@tonic-gate krb5_cc_destroy(handle->context, ccache); 9302881Smp153739 if(handle->clnt && handle->clnt->cl_auth) 9310Sstevel@tonic-gate AUTH_DESTROY(handle->clnt->cl_auth); 9322881Smp153739 if(handle->clnt) 9330Sstevel@tonic-gate clnt_destroy(handle->clnt); 9340Sstevel@tonic-gate (void) kadm5_free_config_params(handle->context, &handle->params); 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate cleanup: 9370Sstevel@tonic-gate if (server) 9380Sstevel@tonic-gate free(server); 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate /* 9411692Ssemery * cred's server and client pointers could have been overwritten 9421692Ssemery * by the krb5_get_init_* functions. If the addresses are different 9431692Ssemery * before and after the calls then we must free the memory that 9441692Ssemery * was allocated before the call. 9450Sstevel@tonic-gate */ 9461692Ssemery if (clientp && clientp != creds.client) 9470Sstevel@tonic-gate krb5_free_principal(handle->context, clientp); 9481692Ssemery 9491692Ssemery if (serverp && serverp != creds.server) 9500Sstevel@tonic-gate krb5_free_principal(handle->context, serverp); 9510Sstevel@tonic-gate 9522881Smp153739 krb5_free_cred_contents(handle->context, &creds); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate /* 9550Sstevel@tonic-gate * Dont clean up the handle if the code is OK (code==0) 9560Sstevel@tonic-gate * because it is returned to the caller in the 'server_handle' 9570Sstevel@tonic-gate * ptr. 9580Sstevel@tonic-gate */ 9592881Smp153739 if (code) { 9600Sstevel@tonic-gate krb5_free_context(handle->context); 9610Sstevel@tonic-gate free(handle->lhandle); 9620Sstevel@tonic-gate free(handle); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9652881Smp153739 return code; 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate kadm5_ret_t 9690Sstevel@tonic-gate kadm5_destroy(void *server_handle) 9700Sstevel@tonic-gate { 9712881Smp153739 krb5_ccache ccache = NULL; 9722881Smp153739 int code = KADM5_OK; 9732881Smp153739 kadm5_server_handle_t handle = 9740Sstevel@tonic-gate (kadm5_server_handle_t) server_handle; 9750Sstevel@tonic-gate OM_uint32 min_stat; 9760Sstevel@tonic-gate 9772881Smp153739 CHECK_HANDLE(server_handle); 9782881Smp153739 /* SUNW14resync: 9792881Smp153739 * krb5_cc_resolve() will resolve a ccache with the same data that 9802881Smp153739 * handle->my_cred points to. If the ccache is a MEMORY ccache then 9812881Smp153739 * gss_release_cred() will free that data (it doesn't do this when ccache 9822881Smp153739 * is a FILE ccache). 9832881Smp153739 * if'ed out to avoid the double free. 9842881Smp153739 */ 9852881Smp153739 #if 0 9862881Smp153739 if (handle->destroy_cache && handle->cache_name) { 9870Sstevel@tonic-gate if ((code = krb5_cc_resolve(handle->context, 9880Sstevel@tonic-gate handle->cache_name, &ccache)) == 0) 9890Sstevel@tonic-gate code = krb5_cc_destroy (handle->context, ccache); 9902881Smp153739 } 9912881Smp153739 #endif 9922881Smp153739 if (handle->cache_name) 9930Sstevel@tonic-gate free(handle->cache_name); 9942881Smp153739 if (handle->clnt && handle->clnt->cl_auth) { 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * Since kadm5 doesn't use the default credentials we 9970Sstevel@tonic-gate * must clean this up manually. 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate if (handle->my_cred != GSS_C_NO_CREDENTIAL) 10000Sstevel@tonic-gate (void) gss_release_cred(&min_stat, &handle->my_cred); 10012881Smp153739 AUTH_DESTROY(handle->clnt->cl_auth); 10020Sstevel@tonic-gate } 10032881Smp153739 if (handle->clnt) 10040Sstevel@tonic-gate clnt_destroy(handle->clnt); 10052881Smp153739 if (handle->lhandle) 10062881Smp153739 free (handle->lhandle); 10072881Smp153739 10082881Smp153739 kadm5_free_config_params(handle->context, &handle->params); 10092881Smp153739 krb5_free_context(handle->context); 10100Sstevel@tonic-gate 10112881Smp153739 handle->magic_number = 0; 10122881Smp153739 free(handle); 10130Sstevel@tonic-gate 10142881Smp153739 return code; 10152881Smp153739 } 10162881Smp153739 /* not supported on client */ 10172881Smp153739 kadm5_ret_t kadm5_lock(void *server_handle) 10182881Smp153739 { 10192881Smp153739 return EINVAL; 10200Sstevel@tonic-gate } 10210Sstevel@tonic-gate 10222881Smp153739 /* not supported on client */ 10232881Smp153739 kadm5_ret_t kadm5_unlock(void *server_handle) 10240Sstevel@tonic-gate { 10252881Smp153739 return EINVAL; 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate 10282881Smp153739 kadm5_ret_t kadm5_flush(void *server_handle) 10290Sstevel@tonic-gate { 10302881Smp153739 return KADM5_OK; 10312881Smp153739 } 10322881Smp153739 10332881Smp153739 int _kadm5_check_handle(void *handle) 10342881Smp153739 { 10352881Smp153739 CHECK_HANDLE(handle); 10362881Smp153739 return 0; 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * Stub function for kadmin. It was created to eliminate the dependency on 10410Sstevel@tonic-gate * libkdb's ulog functions. The srv equivalent makes the actual calls. 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate krb5_error_code 10440Sstevel@tonic-gate kadm5_init_iprop(void *handle) 10450Sstevel@tonic-gate { 10460Sstevel@tonic-gate return (0); 10470Sstevel@tonic-gate } 1048