xref: /titanic_41/usr/src/cmd/krb5/kadmin/server/server_stubs.c (revision b429bb60c66637bc0c5832701cba1df15e075102)
17c478bd9Sstevel@tonic-gate /*
2*b429bb60SZdenek Kotala  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
107c478bd9Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
117c478bd9Sstevel@tonic-gate  *	source code before consulting with your legal department.
127c478bd9Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
137c478bd9Sstevel@tonic-gate  *	product before consulting with your legal department.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *	For further information, read the top-level Openvision
167c478bd9Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
177c478bd9Sstevel@tonic-gate  *	copyright.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
29159d09a2SMark Phalan #include <kadm5/admin.h>
307c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
317c478bd9Sstevel@tonic-gate #include <gssapi_krb5.h>   /* for gss_nt_krb5_name */
327c478bd9Sstevel@tonic-gate #include <kadm5/kadm_rpc.h>
337c478bd9Sstevel@tonic-gate #include <kadm5/server_internal.h>
347c478bd9Sstevel@tonic-gate #include <kadm5/srv/server_acl.h>
357c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <syslog.h>
3856a424ccSmp153739 #include <arpa/inet.h>  /* inet_ntoa */
3956a424ccSmp153739 #include <krb5/adm_proto.h>  /* krb5_klog_syslog */
407c478bd9Sstevel@tonic-gate #include <libintl.h>
41159d09a2SMark Phalan #include <krb5.h>
427c478bd9Sstevel@tonic-gate #include "misc.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define LOG_UNAUTH  gettext("Unauthorized request: %s, %s, " \
457c478bd9Sstevel@tonic-gate 			    "client=%s, service=%s, addr=%s")
467c478bd9Sstevel@tonic-gate #define	LOG_DONE   gettext("Request: %s, %s, %s, client=%s, " \
477c478bd9Sstevel@tonic-gate 			    "service=%s, addr=%s")
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate extern gss_name_t 			gss_changepw_name;
507c478bd9Sstevel@tonic-gate extern gss_name_t			gss_oldchangepw_name;
517c478bd9Sstevel@tonic-gate extern void *				global_server_handle;
527c478bd9Sstevel@tonic-gate extern short l_port;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate char buf[33];
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define CHANGEPW_SERVICE(rqstp) \
577c478bd9Sstevel@tonic-gate 	(cmp_gss_names_rel_1(acceptor_name(rqstp), gss_changepw_name) |\
587c478bd9Sstevel@tonic-gate 	 (gss_oldchangepw_name && \
597c478bd9Sstevel@tonic-gate 	  cmp_gss_names_rel_1(acceptor_name(rqstp), \
607c478bd9Sstevel@tonic-gate 			gss_oldchangepw_name)))
617c478bd9Sstevel@tonic-gate 
6256a424ccSmp153739 
6356a424ccSmp153739 static int gss_to_krb5_name(kadm5_server_handle_t handle,
6456a424ccSmp153739 		     gss_name_t gss_name, krb5_principal *princ);
6556a424ccSmp153739 
6656a424ccSmp153739 static int gss_name_to_string(gss_name_t gss_name, gss_buffer_desc *str);
6756a424ccSmp153739 
6856a424ccSmp153739 static gss_name_t acceptor_name(struct svc_req * rqstp);
6956a424ccSmp153739 
707c478bd9Sstevel@tonic-gate kadm5_ret_t
717c478bd9Sstevel@tonic-gate kadm5_get_priv(void *server_handle,
727c478bd9Sstevel@tonic-gate     long *privs, gss_name_t clnt);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate gss_name_t
get_clnt_name(struct svc_req * rqstp)757c478bd9Sstevel@tonic-gate get_clnt_name(struct svc_req * rqstp)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	OM_uint32 maj_stat, min_stat;
787c478bd9Sstevel@tonic-gate 	gss_name_t name;
797c478bd9Sstevel@tonic-gate 	rpc_gss_rawcred_t *raw_cred;
807c478bd9Sstevel@tonic-gate 	void *cookie;
817c478bd9Sstevel@tonic-gate 	gss_buffer_desc name_buff;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	rpc_gss_getcred(rqstp, &raw_cred, NULL, &cookie);
847c478bd9Sstevel@tonic-gate 	name_buff.value = raw_cred->client_principal->name;
857c478bd9Sstevel@tonic-gate 	name_buff.length = raw_cred->client_principal->len;
867c478bd9Sstevel@tonic-gate 	maj_stat = gss_import_name(&min_stat, &name_buff,
877c478bd9Sstevel@tonic-gate 	    (gss_OID) GSS_C_NT_EXPORT_NAME, &name);
887c478bd9Sstevel@tonic-gate 	if (maj_stat != GSS_S_COMPLETE) {
897c478bd9Sstevel@tonic-gate 		return (NULL);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	return (name);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate char *
client_addr(struct svc_req * req,char * buf)957c478bd9Sstevel@tonic-gate client_addr(struct svc_req * req, char *buf)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	struct sockaddr *ca;
987c478bd9Sstevel@tonic-gate 	u_char *b;
997c478bd9Sstevel@tonic-gate 	char *frontspace = " ";
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * Convert the caller's IP address to a dotted string
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	ca = (struct sockaddr *)
1057c478bd9Sstevel@tonic-gate 	    svc_getrpccaller(req->rq_xprt)->buf;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (ca->sa_family == AF_INET) {
1087c478bd9Sstevel@tonic-gate 		b = (u_char *) & ((struct sockaddr_in *) ca)->sin_addr;
1097c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%s(%d.%d.%d.%d) ", frontspace,
1107c478bd9Sstevel@tonic-gate 		    b[0] & 0xFF, b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
1117c478bd9Sstevel@tonic-gate 	} else {
1127c478bd9Sstevel@tonic-gate 		/*
1137c478bd9Sstevel@tonic-gate 		 * No IP address to print. If there was a host name
1147c478bd9Sstevel@tonic-gate 		 * printed, then we print a space.
1157c478bd9Sstevel@tonic-gate 		 */
1167c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, frontspace);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	return (buf);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
cmp_gss_names(gss_name_t n1,gss_name_t n2)12256a424ccSmp153739 static int cmp_gss_names(gss_name_t n1, gss_name_t n2)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate    OM_uint32 emaj, emin;
1257c478bd9Sstevel@tonic-gate    int equal;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate    if (GSS_ERROR(emaj = gss_compare_name(&emin, n1, n2, &equal)))
1287c478bd9Sstevel@tonic-gate       return(0);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate    return(equal);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* Does a comparison of the names and then releases the first entity */
1347c478bd9Sstevel@tonic-gate /* For use above in CHANGEPW_SERVICE */
cmp_gss_names_rel_1(gss_name_t n1,gss_name_t n2)13556a424ccSmp153739 static int cmp_gss_names_rel_1(gss_name_t n1, gss_name_t n2)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate    OM_uint32 min_stat;
1387c478bd9Sstevel@tonic-gate    int ret;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate    ret = cmp_gss_names(n1, n2);
1417c478bd9Sstevel@tonic-gate    if (n1) (void) gss_release_name(&min_stat, &n1);
1427c478bd9Sstevel@tonic-gate    return ret;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Function check_handle
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  * Purpose: Check a server handle and return a com_err code if it is
1497c478bd9Sstevel@tonic-gate  * invalid or 0 if it is valid.
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  * Arguments:
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * 	handle		The server handle.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate 
check_handle(void * handle)15656a424ccSmp153739 static int check_handle(void *handle)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate      CHECK_HANDLE(handle);
15956a424ccSmp153739      return 0;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Function: new_server_handle
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  * Purpose: Constructs a server handle suitable for passing into the
1667c478bd9Sstevel@tonic-gate  * server library API functions, by folding the client's API version
1677c478bd9Sstevel@tonic-gate  * and calling principal into the server handle returned by
1687c478bd9Sstevel@tonic-gate  * kadm5_init.
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * Arguments:
1717c478bd9Sstevel@tonic-gate  * 	api_version	(input) The API version specified by the client
1727c478bd9Sstevel@tonic-gate  * 	rqstp		(input) The RPC request
1737c478bd9Sstevel@tonic-gate  * 	handle		(output) The returned handle
1747c478bd9Sstevel@tonic-gate  *	<return value>	(output) An error code, or 0 if no error occurred
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * Effects:
1777c478bd9Sstevel@tonic-gate  * 	Returns a pointer to allocated storage containing the server
1787c478bd9Sstevel@tonic-gate  * 	handle.  If an error occurs, then no allocated storage is
1797c478bd9Sstevel@tonic-gate  *	returned, and the return value of the function will be a
1807c478bd9Sstevel@tonic-gate  * 	non-zero com_err code.
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  *      The allocated storage for the handle should be freed with
1837c478bd9Sstevel@tonic-gate  * 	free_server_handle (see below) when it is no longer needed.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate 
new_server_handle(krb5_ui_4 api_version,struct svc_req * rqstp,kadm5_server_handle_t * out_handle)18656a424ccSmp153739 static kadm5_ret_t new_server_handle(krb5_ui_4 api_version,
1877c478bd9Sstevel@tonic-gate 					  struct svc_req *rqstp,
18856a424ccSmp153739 					  kadm5_server_handle_t
18956a424ccSmp153739 					  *out_handle)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate      kadm5_server_handle_t handle;
1927c478bd9Sstevel@tonic-gate 	gss_name_t name;
1937c478bd9Sstevel@tonic-gate 	OM_uint32 min_stat;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate      if (! (handle = (kadm5_server_handle_t)
1967c478bd9Sstevel@tonic-gate 	    malloc(sizeof(*handle))))
19756a424ccSmp153739 	  return ENOMEM;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate      *handle = *(kadm5_server_handle_t)global_server_handle;
2007c478bd9Sstevel@tonic-gate      handle->api_version = api_version;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate      if (!(name = get_clnt_name(rqstp))) {
2037c478bd9Sstevel@tonic-gate 	  free(handle);
20456a424ccSmp153739 	  return KADM5_FAILURE;
2057c478bd9Sstevel@tonic-gate      }
2067c478bd9Sstevel@tonic-gate     if (! gss_to_krb5_name(handle, name, &handle->current_caller)) {
2077c478bd9Sstevel@tonic-gate 	  free(handle);
2087c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
20956a424ccSmp153739 	  return KADM5_FAILURE;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	gss_release_name(&min_stat, &name);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate      *out_handle = handle;
21456a424ccSmp153739      return 0;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * Function: free_server_handle
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  * Purpose: Free handle memory allocated by new_server_handle
2217c478bd9Sstevel@tonic-gate  *
2227c478bd9Sstevel@tonic-gate  * Arguments:
2237c478bd9Sstevel@tonic-gate  * 	handle		(input/output) The handle to free
2247c478bd9Sstevel@tonic-gate  */
free_server_handle(kadm5_server_handle_t handle)22556a424ccSmp153739 static void free_server_handle(kadm5_server_handle_t handle)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate      krb5_free_principal(handle->context, handle->current_caller);
2287c478bd9Sstevel@tonic-gate      free(handle);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Function: setup_gss_names
2337c478bd9Sstevel@tonic-gate  *
2347c478bd9Sstevel@tonic-gate  * Purpose: Create printable representations of the client and server
2357c478bd9Sstevel@tonic-gate  * names.
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  * Arguments:
2387c478bd9Sstevel@tonic-gate  * 	rqstp		(r) the RPC request
2397c478bd9Sstevel@tonic-gate  * 	client_name	(w) pointer to client_name string
2407c478bd9Sstevel@tonic-gate  * 	server_name	(w) pointer to server_name string
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  * Effects:
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  * Unparses the client and server names into client_name and
2457c478bd9Sstevel@tonic-gate  * server_name, both of which must be freed by the caller.  Returns 0
2467c478bd9Sstevel@tonic-gate  * on success and -1 on failure. On failure client_name and server_name
2477c478bd9Sstevel@tonic-gate  * will point to null.
2487c478bd9Sstevel@tonic-gate  */
24956a424ccSmp153739 /* SUNW14resync */
setup_gss_names(struct svc_req * rqstp,char ** client_name,char ** server_name)25056a424ccSmp153739 int setup_gss_names(struct svc_req *rqstp,
2517c478bd9Sstevel@tonic-gate     char **client_name, char **server_name)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate      OM_uint32 maj_stat, min_stat;
2547c478bd9Sstevel@tonic-gate 	rpc_gss_rawcred_t *raw_cred;
2557c478bd9Sstevel@tonic-gate 	gss_buffer_desc name_buf;
2567c478bd9Sstevel@tonic-gate 	char *tmp, *val;
2577c478bd9Sstevel@tonic-gate 	size_t len;
2587c478bd9Sstevel@tonic-gate 	gss_name_t name;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	*client_name = NULL;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	rpc_gss_getcred(rqstp, &raw_cred, NULL, NULL);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* Return a copy of the service principal from the raw_cred */
2657c478bd9Sstevel@tonic-gate 	*server_name = strdup(raw_cred->svc_principal);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (*server_name == NULL)
2687c478bd9Sstevel@tonic-gate 		return (-1);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
2717c478bd9Sstevel@tonic-gate 		free(*server_name);
2727c478bd9Sstevel@tonic-gate 		*server_name = NULL;
2737c478bd9Sstevel@tonic-gate 		return (-1);
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	maj_stat = gss_display_name(&min_stat, name, &name_buf, NULL);
2767c478bd9Sstevel@tonic-gate 	if (maj_stat != GSS_S_COMPLETE) {
2777c478bd9Sstevel@tonic-gate 		free(*server_name);
2787c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
2797c478bd9Sstevel@tonic-gate 		*server_name = NULL;
2807c478bd9Sstevel@tonic-gate 		return (-1);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 	gss_release_name(&min_stat, &name);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * Allocate space to copy the client principal. We allocate an
2867c478bd9Sstevel@tonic-gate 	 * extra byte to make the string null terminated if we need to.
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	val = name_buf.value;
2907c478bd9Sstevel@tonic-gate 	len = name_buf.length + (val[name_buf.length - 1] != '\0');
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/* len is the length including the null terminating byte. */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	tmp = malloc(len);
2957c478bd9Sstevel@tonic-gate 	if (tmp) {
2967c478bd9Sstevel@tonic-gate 		memcpy(tmp, val, len - 1);
2977c478bd9Sstevel@tonic-gate 		tmp[len - 1] = '\0';
2987c478bd9Sstevel@tonic-gate 	} else {
2997c478bd9Sstevel@tonic-gate 		free(*server_name);
3007c478bd9Sstevel@tonic-gate 		*server_name = NULL;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/* Were done with the GSS buffer */
3047c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&min_stat, &name_buf);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	*client_name = tmp;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (tmp ? 0 : -1);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
acceptor_name(struct svc_req * rqstp)31156a424ccSmp153739 static gss_name_t acceptor_name(struct svc_req * rqstp)
31256a424ccSmp153739 {
31356a424ccSmp153739      OM_uint32 maj_stat, min_stat;
31456a424ccSmp153739      gss_name_t name;
31556a424ccSmp153739      rpc_gss_rawcred_t *raw_cred;
31656a424ccSmp153739      void *cookie;
31756a424ccSmp153739      gss_buffer_desc name_buff;
31856a424ccSmp153739 
31956a424ccSmp153739 	rpc_gss_getcred(rqstp, &raw_cred, NULL, &cookie);
32056a424ccSmp153739 	name_buff.value = raw_cred->svc_principal;
32156a424ccSmp153739 	name_buff.length = strlen(raw_cred->svc_principal);
32256a424ccSmp153739 	maj_stat = gss_import_name(&min_stat, &name_buff,
32356a424ccSmp153739 	    (gss_OID) gss_nt_krb5_name, &name);
32456a424ccSmp153739 	if (maj_stat != GSS_S_COMPLETE) {
32556a424ccSmp153739 		gss_release_buffer(&min_stat, &name_buff);
32656a424ccSmp153739 		return (NULL);
32756a424ccSmp153739 	}
32856a424ccSmp153739 	maj_stat = gss_display_name(&min_stat, name, &name_buff, NULL);
32956a424ccSmp153739     if (maj_stat != GSS_S_COMPLETE) {
33056a424ccSmp153739 		gss_release_buffer(&min_stat, &name_buff);
33156a424ccSmp153739 	  return (NULL);
33256a424ccSmp153739 	}
33356a424ccSmp153739 	gss_release_buffer(&min_stat, &name_buff);
33456a424ccSmp153739 
33556a424ccSmp153739      return name;
33656a424ccSmp153739 }
33756a424ccSmp153739 
cmp_gss_krb5_name(kadm5_server_handle_t handle,gss_name_t gss_name,krb5_principal princ)33856a424ccSmp153739 static int cmp_gss_krb5_name(kadm5_server_handle_t handle,
3397c478bd9Sstevel@tonic-gate 		      gss_name_t gss_name, krb5_principal princ)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate      krb5_principal princ2;
34256a424ccSmp153739      int status;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate      if (! gss_to_krb5_name(handle, gss_name, &princ2))
34556a424ccSmp153739 	  return 0;
34656a424ccSmp153739      status = krb5_principal_compare(handle->context, princ, princ2);
3477c478bd9Sstevel@tonic-gate      krb5_free_principal(handle->context, princ2);
34856a424ccSmp153739      return status;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * This routine primarily validates the username and password
3547c478bd9Sstevel@tonic-gate  * of the principal to be created, if a prior acl check for
3557c478bd9Sstevel@tonic-gate  * the 'u' privilege succeeds. Validation is done using
3567c478bd9Sstevel@tonic-gate  * the PAM `k5migrate' service. k5migrate normally stacks
3577c478bd9Sstevel@tonic-gate  * pam_unix_auth.so and pam_unix_account.so in its auth and
3587c478bd9Sstevel@tonic-gate  * account stacks respectively.
3597c478bd9Sstevel@tonic-gate  *
3607c478bd9Sstevel@tonic-gate  * Returns 1 (true), if validation is successful,
3617c478bd9Sstevel@tonic-gate  * else returns 0 (false).
3627c478bd9Sstevel@tonic-gate  */
verify_pam_pw(char * userdata,char * pwd)3637c478bd9Sstevel@tonic-gate int verify_pam_pw(char *userdata, char *pwd) {
3647c478bd9Sstevel@tonic-gate 	pam_handle_t *pamh;
3657c478bd9Sstevel@tonic-gate 	int err = 0;
3667c478bd9Sstevel@tonic-gate 	int result = 1;
3677c478bd9Sstevel@tonic-gate 	char *user = NULL;
3687c478bd9Sstevel@tonic-gate 	char *ptr = NULL;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	ptr = strchr(userdata, '@');
3717c478bd9Sstevel@tonic-gate 	if (ptr != NULL) {
3727c478bd9Sstevel@tonic-gate 		user = (char *)malloc(ptr - userdata + 1);
3737c478bd9Sstevel@tonic-gate 		(void) strlcpy(user, userdata, (ptr - userdata) + 1);
3747c478bd9Sstevel@tonic-gate 	} else {
3757c478bd9Sstevel@tonic-gate 		user = (char *)strdup(userdata);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	err = pam_start("k5migrate", user, NULL, &pamh);
3797c478bd9Sstevel@tonic-gate 	if (err != PAM_SUCCESS) {
3807c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "verify_pam_pw: pam_start() failed, %s\n",
3817c478bd9Sstevel@tonic-gate 				pam_strerror(pamh, err));
3827c478bd9Sstevel@tonic-gate 		if (user)
3837c478bd9Sstevel@tonic-gate 			free(user);
3847c478bd9Sstevel@tonic-gate 		return (0);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 	if (user)
3877c478bd9Sstevel@tonic-gate 		free(user);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	err = pam_set_item(pamh, PAM_AUTHTOK, (void *)pwd);
3907c478bd9Sstevel@tonic-gate 	if (err != PAM_SUCCESS) {
3917c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "verify_pam_pw: pam_set_item() failed, %s\n",
3927c478bd9Sstevel@tonic-gate 				pam_strerror(pamh, err));
3937c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, err);
3947c478bd9Sstevel@tonic-gate 		return (0);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	err = pam_authenticate(pamh, PAM_SILENT);
3987c478bd9Sstevel@tonic-gate 	if (err != PAM_SUCCESS) {
3997c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "verify_pam_pw: pam_authenticate() "
4007c478bd9Sstevel@tonic-gate 				"failed, %s\n", pam_strerror(pamh, err));
4017c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, err);
4027c478bd9Sstevel@tonic-gate 		return (0);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	err = pam_acct_mgmt(pamh, PAM_SILENT);
4067c478bd9Sstevel@tonic-gate 	if (err != PAM_SUCCESS) {
4077c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "verify_pam_pw: pam_acct_mgmt() failed, %s\n",
4087c478bd9Sstevel@tonic-gate 				pam_strerror(pamh, err));
4097c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, err);
4107c478bd9Sstevel@tonic-gate 		return (0);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_SUCCESS);
4147c478bd9Sstevel@tonic-gate 	return (result);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
gss_to_krb5_name(kadm5_server_handle_t handle,gss_name_t gss_name,krb5_principal * princ)41756a424ccSmp153739 static int gss_to_krb5_name(kadm5_server_handle_t handle,
41856a424ccSmp153739 		     gss_name_t gss_name, krb5_principal *princ)
41956a424ccSmp153739 {
42056a424ccSmp153739      OM_uint32 status, minor_stat;
42156a424ccSmp153739      gss_buffer_desc gss_str;
42256a424ccSmp153739      gss_OID gss_type;
42356a424ccSmp153739      int success;
42456a424ccSmp153739 
42556a424ccSmp153739      status = gss_display_name(&minor_stat, gss_name, &gss_str, &gss_type);
42656a424ccSmp153739      if ((status != GSS_S_COMPLETE) || (!g_OID_equal(gss_type, gss_nt_krb5_name)))
42756a424ccSmp153739 	  return 0;
42856a424ccSmp153739      success = (krb5_parse_name(handle->context, gss_str.value, princ) == 0);
42956a424ccSmp153739      gss_release_buffer(&minor_stat, &gss_str);
43056a424ccSmp153739      return success;
43156a424ccSmp153739 }
43256a424ccSmp153739 
43356a424ccSmp153739 static int
gss_name_to_string(gss_name_t gss_name,gss_buffer_desc * str)43456a424ccSmp153739 gss_name_to_string(gss_name_t gss_name, gss_buffer_desc *str)
43556a424ccSmp153739 {
43656a424ccSmp153739      OM_uint32 status, minor_stat;
43756a424ccSmp153739      gss_OID gss_type;
43856a424ccSmp153739 
43956a424ccSmp153739      status = gss_display_name(&minor_stat, gss_name, str, &gss_type);
44056a424ccSmp153739      if ((status != GSS_S_COMPLETE) || (gss_type != gss_nt_krb5_name))
44156a424ccSmp153739 	  return 1;
44256a424ccSmp153739      return 0;
44356a424ccSmp153739 }
44456a424ccSmp153739 
44546736d35Ssemery static int
log_unauth(char * op,char * target,char * client,char * server,char * addr)44646736d35Ssemery log_unauth(
44746736d35Ssemery     char *op,
44846736d35Ssemery     char *target,
44946736d35Ssemery     char *client,
45046736d35Ssemery     char *server,
45146736d35Ssemery     char *addr)
45246736d35Ssemery {
45346736d35Ssemery     size_t tlen, clen, slen;
45446736d35Ssemery     char *tdots, *cdots, *sdots;
45546736d35Ssemery 
45646736d35Ssemery     tlen = strlen(target);
45746736d35Ssemery     trunc_name(&tlen, &tdots);
45846736d35Ssemery     clen = strlen(client);
45946736d35Ssemery     trunc_name(&clen, &cdots);
46046736d35Ssemery     slen = strlen(server);
46146736d35Ssemery     trunc_name(&slen, &sdots);
46246736d35Ssemery 
46346736d35Ssemery     return krb5_klog_syslog(LOG_NOTICE,
46446736d35Ssemery 			    "Unauthorized request: %s, %.*s%s, "
46546736d35Ssemery 			    "client=%.*s%s, service=%.*s%s, addr=%s",
46646736d35Ssemery 			    op, tlen, target, tdots,
46746736d35Ssemery 			    clen, client, cdots,
46846736d35Ssemery 			    slen, server, sdots,
46946736d35Ssemery 			    addr);
47046736d35Ssemery }
47146736d35Ssemery 
47246736d35Ssemery static int
log_done(char * op,char * target,const char * errmsg,char * client,char * server,char * addr)47346736d35Ssemery log_done(
47446736d35Ssemery     char *op,
47546736d35Ssemery     char *target,
47646736d35Ssemery     const char *errmsg,
47746736d35Ssemery     char *client,
47846736d35Ssemery     char *server,
47946736d35Ssemery     char *addr)
48046736d35Ssemery {
48146736d35Ssemery     size_t tlen, clen, slen;
48246736d35Ssemery     char *tdots, *cdots, *sdots;
48346736d35Ssemery 
48446736d35Ssemery     tlen = strlen(target);
48546736d35Ssemery     trunc_name(&tlen, &tdots);
48646736d35Ssemery     clen = strlen(client);
48746736d35Ssemery     trunc_name(&clen, &cdots);
48846736d35Ssemery     slen = strlen(server);
48946736d35Ssemery     trunc_name(&slen, &sdots);
49046736d35Ssemery 
49146736d35Ssemery     return krb5_klog_syslog(LOG_NOTICE,
49246736d35Ssemery 			    "Request: %s, %.*s%s, %s, "
49346736d35Ssemery 			    "client=%.*s%s, service=%.*s%s, addr=%s",
49446736d35Ssemery 			    op, tlen, target, tdots, errmsg,
49546736d35Ssemery 			    clen, client, cdots,
49646736d35Ssemery 			    slen, server, sdots,
49746736d35Ssemery 			    addr);
49846736d35Ssemery }
49946736d35Ssemery 
5007c478bd9Sstevel@tonic-gate generic_ret *
create_principal_2_svc(cprinc_arg * arg,struct svc_req * rqstp)501159d09a2SMark Phalan create_principal_2_svc(cprinc_arg *arg, struct svc_req *rqstp)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate     static generic_ret		ret;
5047c478bd9Sstevel@tonic-gate     char			*prime_arg = NULL;
5057c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
5067c478bd9Sstevel@tonic-gate     int policy_migrate = 0;
5077c478bd9Sstevel@tonic-gate 
50856a424ccSmp153739     OM_uint32			minor_stat;
5097c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	handle;
5107c478bd9Sstevel@tonic-gate     kadm5_ret_t retval;
5117c478bd9Sstevel@tonic-gate     restriction_t		*rp;
512159d09a2SMark Phalan     const char			*errmsg = NULL;
5137c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
5167c478bd9Sstevel@tonic-gate 
51756a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
51856a424ccSmp153739 	 return &ret;
5197c478bd9Sstevel@tonic-gate 
52056a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
5217c478bd9Sstevel@tonic-gate 		goto error;
5227c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
5257c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
5267c478bd9Sstevel@tonic-gate 	goto error;
5277c478bd9Sstevel@tonic-gate     }
52856a424ccSmp153739     if (krb5_unparse_name(handle->context, arg->rec.principal, &prime_arg)) {
5297c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
5307c478bd9Sstevel@tonic-gate 	 goto error;
5317c478bd9Sstevel@tonic-gate     }
5327c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
5337c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
5347c478bd9Sstevel@tonic-gate 		goto error;
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
53756a424ccSmp153739 	if (kadm5int_acl_check(handle->context, name, ACL_MIGRATE,
5387c478bd9Sstevel@tonic-gate 	    arg->rec.principal, &rp) &&
5397c478bd9Sstevel@tonic-gate 	    verify_pam_pw(prime_arg, arg->passwd)) {
5407c478bd9Sstevel@tonic-gate 		policy_migrate = 1;
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate     if (CHANGEPW_SERVICE(rqstp)
54456a424ccSmp153739 	|| (!kadm5int_acl_check(handle->context, name, ACL_ADD,
5457c478bd9Sstevel@tonic-gate 			arg->rec.principal, &rp) &&
5467c478bd9Sstevel@tonic-gate 		!(policy_migrate))
54756a424ccSmp153739 	|| kadm5int_acl_impose_restrictions(handle->context,
5487c478bd9Sstevel@tonic-gate 				   &arg->rec, &arg->mask, rp)) {
5497c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_ADD;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
5527c478bd9Sstevel@tonic-gate 				    "kadm5_create_principal",
5537c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
55446736d35Ssemery 	 log_unauth("kadm5_create_principal", prime_arg,
55546736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
5567c478bd9Sstevel@tonic-gate     } else {
5577c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_create_principal((void *)handle,
5587c478bd9Sstevel@tonic-gate 						&arg->rec, arg->mask,
5597c478bd9Sstevel@tonic-gate 						arg->passwd);
560159d09a2SMark Phalan 	/* Solaris Kerberos */
561159d09a2SMark Phalan 	 if( ret.code != 0 )
562159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
5657c478bd9Sstevel@tonic-gate 				"kadm5_create_principal",
5667c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
56746736d35Ssemery 	 log_done("kadm5_create_principal", prime_arg,
568159d09a2SMark Phalan 	    errmsg ? errmsg : "success",
5697c478bd9Sstevel@tonic-gate 	    client_name, service_name, client_addr(rqstp, buf));
5707c478bd9Sstevel@tonic-gate 
571159d09a2SMark Phalan 	  if (errmsg != NULL)
572159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
573159d09a2SMark Phalan 
5747c478bd9Sstevel@tonic-gate 		if (policy_migrate && (ret.code == 0)) {
5757c478bd9Sstevel@tonic-gate 			arg->rec.policy = strdup("default");
5767c478bd9Sstevel@tonic-gate 			if ((arg->mask & KADM5_PW_EXPIRATION)) {
5777c478bd9Sstevel@tonic-gate 				arg->mask = 0;
5787c478bd9Sstevel@tonic-gate 				arg->mask |= KADM5_POLICY;
5797c478bd9Sstevel@tonic-gate 				arg->mask |= KADM5_PW_EXPIRATION;
5807c478bd9Sstevel@tonic-gate 			} else {
5817c478bd9Sstevel@tonic-gate 				arg->mask = 0;
5827c478bd9Sstevel@tonic-gate 				arg->mask |= KADM5_POLICY;
5837c478bd9Sstevel@tonic-gate 			}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 			retval = kadm5_modify_principal((void *)handle,
5867c478bd9Sstevel@tonic-gate 					&arg->rec, arg->mask);
58746736d35Ssemery 			log_done("kadm5_modify_principal",
5887c478bd9Sstevel@tonic-gate 				prime_arg, ((retval == 0) ? "success" :
5897c478bd9Sstevel@tonic-gate 				error_message(retval)), client_name,
5907c478bd9Sstevel@tonic-gate 				service_name, client_addr(rqstp, buf));
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate error:
5957c478bd9Sstevel@tonic-gate     if (name)
59656a424ccSmp153739     	gss_release_name(&minor_stat, &name);
5977c478bd9Sstevel@tonic-gate     free_server_handle(handle);
5987c478bd9Sstevel@tonic-gate     if (prime_arg)
5997c478bd9Sstevel@tonic-gate     	free(prime_arg);
6007c478bd9Sstevel@tonic-gate     if (client_name)
6017c478bd9Sstevel@tonic-gate     	free(client_name);
6027c478bd9Sstevel@tonic-gate     if (service_name)
6037c478bd9Sstevel@tonic-gate     	free(service_name);
6047c478bd9Sstevel@tonic-gate     return (&ret);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate generic_ret *
create_principal3_2_svc(cprinc3_arg * arg,struct svc_req * rqstp)608159d09a2SMark Phalan create_principal3_2_svc(cprinc3_arg *arg, struct svc_req *rqstp)
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate     static generic_ret		ret;
6117c478bd9Sstevel@tonic-gate     char			*prime_arg = NULL;
6127c478bd9Sstevel@tonic-gate     char			*client_name = NULL, *service_name = NULL;
6137c478bd9Sstevel@tonic-gate     int				policy_migrate = 0;
6147c478bd9Sstevel@tonic-gate 
61556a424ccSmp153739     OM_uint32			minor_stat;
6167c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	handle;
6177c478bd9Sstevel@tonic-gate     kadm5_ret_t			retval;
6187c478bd9Sstevel@tonic-gate     restriction_t		*rp;
619159d09a2SMark Phalan     const char                        *errmsg = NULL;
6207c478bd9Sstevel@tonic-gate     gss_name_t			name = NULL;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
6237c478bd9Sstevel@tonic-gate 
62456a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
6257c478bd9Sstevel@tonic-gate 	 return &ret;
6267c478bd9Sstevel@tonic-gate 
62756a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
6287c478bd9Sstevel@tonic-gate 	goto error;
6297c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
6327c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
6337c478bd9Sstevel@tonic-gate 	goto error;
6347c478bd9Sstevel@tonic-gate     }
6357c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->rec.principal, &prime_arg)) {
6367c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
6377c478bd9Sstevel@tonic-gate 	goto error;
6387c478bd9Sstevel@tonic-gate     }
6397c478bd9Sstevel@tonic-gate     if (!(name = get_clnt_name(rqstp))) {
6407c478bd9Sstevel@tonic-gate 	ret.code = KADM5_FAILURE;
6417c478bd9Sstevel@tonic-gate 	goto error;
6427c478bd9Sstevel@tonic-gate     }
6437c478bd9Sstevel@tonic-gate 
64456a424ccSmp153739     if (kadm5int_acl_check(handle->context, name, ACL_MIGRATE,
6457c478bd9Sstevel@tonic-gate 		arg->rec.principal, &rp) &&
6467c478bd9Sstevel@tonic-gate 		verify_pam_pw(prime_arg, arg->passwd)) {
6477c478bd9Sstevel@tonic-gate 	policy_migrate = 1;
6487c478bd9Sstevel@tonic-gate     }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate     if (CHANGEPW_SERVICE(rqstp)
65156a424ccSmp153739 	|| (!kadm5int_acl_check(handle->context, name, ACL_ADD,
6527c478bd9Sstevel@tonic-gate 			arg->rec.principal, &rp) &&
6537c478bd9Sstevel@tonic-gate 	    !(policy_migrate))
65456a424ccSmp153739 	|| kadm5int_acl_impose_restrictions(handle->context,
6557c478bd9Sstevel@tonic-gate 				   &arg->rec, &arg->mask, rp)) {
6567c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_ADD;
65746736d35Ssemery 	 log_unauth("kadm5_create_principal", prime_arg,
65846736d35Ssemery 		    client_name, service_name, client_addr(rqstp, buf));
6597c478bd9Sstevel@tonic-gate     } else {
6607c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_create_principal_3((void *)handle,
6617c478bd9Sstevel@tonic-gate 					     &arg->rec, arg->mask,
6627c478bd9Sstevel@tonic-gate 					     arg->n_ks_tuple,
6637c478bd9Sstevel@tonic-gate 					     arg->ks_tuple,
6647c478bd9Sstevel@tonic-gate 					     arg->passwd);
665159d09a2SMark Phalan 	/* Solaris Kerberos */
666159d09a2SMark Phalan 	 if( ret.code != 0 )
667159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
668159d09a2SMark Phalan 
66946736d35Ssemery 	 log_done("kadm5_create_principal", prime_arg,
670159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
67146736d35Ssemery 		  client_name, service_name, client_addr(rqstp, buf));
6727c478bd9Sstevel@tonic-gate 
673159d09a2SMark Phalan 	  if (errmsg != NULL)
674159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
675159d09a2SMark Phalan 
6767c478bd9Sstevel@tonic-gate 	 if (policy_migrate && (ret.code == 0)) {
6777c478bd9Sstevel@tonic-gate 	 	arg->rec.policy = strdup("default");
6787c478bd9Sstevel@tonic-gate 	 	if ((arg->mask & KADM5_PW_EXPIRATION)) {
6797c478bd9Sstevel@tonic-gate 	 		arg->mask = 0;
6807c478bd9Sstevel@tonic-gate 	 		arg->mask |= KADM5_POLICY;
6817c478bd9Sstevel@tonic-gate 	 		arg->mask |= KADM5_PW_EXPIRATION;
6827c478bd9Sstevel@tonic-gate 	 	} else {
6837c478bd9Sstevel@tonic-gate 	 		arg->mask = 0;
6847c478bd9Sstevel@tonic-gate 	 		arg->mask |= KADM5_POLICY;
6857c478bd9Sstevel@tonic-gate 	 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		retval = kadm5_modify_principal((void *)handle,
6887c478bd9Sstevel@tonic-gate 					   &arg->rec, arg->mask);
68946736d35Ssemery 		log_done("kadm5_modify_principal", prime_arg,
69046736d35Ssemery 			((retval == 0) ? "success" : error_message(retval)),
69146736d35Ssemery 			client_name, service_name, client_addr(rqstp, buf));
6927c478bd9Sstevel@tonic-gate 	 }
6937c478bd9Sstevel@tonic-gate     }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate error:
6967c478bd9Sstevel@tonic-gate     if (name)
69756a424ccSmp153739     	gss_release_name(&minor_stat, &name);
6987c478bd9Sstevel@tonic-gate     free_server_handle(handle);
6997c478bd9Sstevel@tonic-gate     if (client_name)
7007c478bd9Sstevel@tonic-gate     	free(client_name);
7017c478bd9Sstevel@tonic-gate     if (service_name)
7027c478bd9Sstevel@tonic-gate     	free(service_name);
7037c478bd9Sstevel@tonic-gate     if (prime_arg)
7047c478bd9Sstevel@tonic-gate     	free(prime_arg);
70556a424ccSmp153739     return &ret;
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate generic_ret *
delete_principal_2_svc(dprinc_arg * arg,struct svc_req * rqstp)709159d09a2SMark Phalan delete_principal_2_svc(dprinc_arg *arg, struct svc_req *rqstp)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
7127c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
7137c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
7147c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
7157c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
716159d09a2SMark Phalan     const char                            *errmsg = NULL;
717159d09a2SMark Phalan 
7187c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
7197c478bd9Sstevel@tonic-gate 
720159d09a2SMark Phalan 
7217c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
7227c478bd9Sstevel@tonic-gate 
72356a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
72456a424ccSmp153739 	 return &ret;
7257c478bd9Sstevel@tonic-gate 
72656a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
7277c478bd9Sstevel@tonic-gate 		goto error;
7287c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
7317c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
7327c478bd9Sstevel@tonic-gate 		goto error;
7337c478bd9Sstevel@tonic-gate     }
7347c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
7357c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
7367c478bd9Sstevel@tonic-gate 		goto error;
7377c478bd9Sstevel@tonic-gate     }
7387c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
7397c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
7407c478bd9Sstevel@tonic-gate 		goto error;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate     if (CHANGEPW_SERVICE(rqstp)
74456a424ccSmp153739 	|| !kadm5int_acl_check(handle->context, name, ACL_DELETE,
7457c478bd9Sstevel@tonic-gate 		      arg->princ, NULL)) {
7467c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_DELETE;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
7497c478bd9Sstevel@tonic-gate 				    "kadm5_delete_principal",
7507c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
75146736d35Ssemery 	 log_unauth("kadm5_delete_principal", prime_arg, client_name,
7527c478bd9Sstevel@tonic-gate 			service_name, client_addr(rqstp, buf));
7537c478bd9Sstevel@tonic-gate     } else {
7547c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_delete_principal((void *)handle, arg->princ);
755159d09a2SMark Phalan 	/* Solaris Kerberos */
756159d09a2SMark Phalan 	 if( ret.code != 0 )
757159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
7607c478bd9Sstevel@tonic-gate 				"kadm5_delete_principal",
7617c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
76246736d35Ssemery 	 log_done("kadm5_delete_principal", prime_arg,
763159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
7647c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
765159d09a2SMark Phalan 
766159d09a2SMark Phalan 	  if (errmsg != NULL)
767159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
768159d09a2SMark Phalan 
7697c478bd9Sstevel@tonic-gate     }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate error:
7727c478bd9Sstevel@tonic-gate     if (name)
7737c478bd9Sstevel@tonic-gate     	gss_release_name(&min_stat, &name);
7747c478bd9Sstevel@tonic-gate     if (prime_arg)
7757c478bd9Sstevel@tonic-gate     	free(prime_arg);
7767c478bd9Sstevel@tonic-gate     free_server_handle(handle);
7777c478bd9Sstevel@tonic-gate     if (client_name)
7787c478bd9Sstevel@tonic-gate     	free(client_name);
7797c478bd9Sstevel@tonic-gate     if (service_name)
7807c478bd9Sstevel@tonic-gate     	free(service_name);
78156a424ccSmp153739     return &ret;
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate generic_ret *
modify_principal_2_svc(mprinc_arg * arg,struct svc_req * rqstp)785159d09a2SMark Phalan modify_principal_2_svc(mprinc_arg *arg, struct svc_req *rqstp)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
7887c478bd9Sstevel@tonic-gate     char *prime_arg = NULL;
7897c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
7907c478bd9Sstevel@tonic-gate     OM_uint32 min_stat;
7917c478bd9Sstevel@tonic-gate     kadm5_server_handle_t handle;
7927c478bd9Sstevel@tonic-gate     restriction_t *rp;
7937c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
794159d09a2SMark Phalan     const char                            *errmsg = NULL;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
7977c478bd9Sstevel@tonic-gate 
79856a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
79956a424ccSmp153739 	 return &ret;
8007c478bd9Sstevel@tonic-gate 
80156a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
8027c478bd9Sstevel@tonic-gate 		goto error;
8037c478bd9Sstevel@tonic-gate    if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
8047c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
8057c478bd9Sstevel@tonic-gate 		goto error;
8067c478bd9Sstevel@tonic-gate     }
80756a424ccSmp153739     if (krb5_unparse_name(handle->context, arg->rec.principal, &prime_arg)) {
8087c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
8097c478bd9Sstevel@tonic-gate 	 goto error;
8107c478bd9Sstevel@tonic-gate     }
8117c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
8127c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
8137c478bd9Sstevel@tonic-gate 		goto error;
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate     if (CHANGEPW_SERVICE(rqstp)
81756a424ccSmp153739 	|| !kadm5int_acl_check(handle->context, name, ACL_MODIFY,
8187c478bd9Sstevel@tonic-gate 		      arg->rec.principal, &rp)
81956a424ccSmp153739 	|| kadm5int_acl_impose_restrictions(handle->context,
8207c478bd9Sstevel@tonic-gate 				   &arg->rec, &arg->mask, rp)) {
8217c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_MODIFY;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
8247c478bd9Sstevel@tonic-gate 				    "kadm5_modify_principal",
8257c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
82646736d35Ssemery 	 log_unauth("kadm5_modify_principal", prime_arg, client_name,
8277c478bd9Sstevel@tonic-gate 		    service_name, client_addr(rqstp, buf));
8287c478bd9Sstevel@tonic-gate     } else {
8297c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_modify_principal((void *)handle, &arg->rec,
8307c478bd9Sstevel@tonic-gate 						arg->mask);
831159d09a2SMark Phalan 	/* Solaris Kerberos */
832159d09a2SMark Phalan 	 if( ret.code != 0 )
833159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
8367c478bd9Sstevel@tonic-gate 				"kadm5_modify_principal",
8377c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
83846736d35Ssemery 	 log_done("kadm5_modify_principal", prime_arg,
839159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
8407c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
841159d09a2SMark Phalan 
842159d09a2SMark Phalan 	  if (errmsg != NULL)
843159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
8447c478bd9Sstevel@tonic-gate     }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate error:
8477c478bd9Sstevel@tonic-gate     if (name)
8487c478bd9Sstevel@tonic-gate     	gss_release_name(&min_stat, &name);
8497c478bd9Sstevel@tonic-gate     free_server_handle(handle);
8507c478bd9Sstevel@tonic-gate     if (prime_arg)
8517c478bd9Sstevel@tonic-gate     	free(prime_arg);
8527c478bd9Sstevel@tonic-gate     if (client_name)
8537c478bd9Sstevel@tonic-gate     	free(client_name);
8547c478bd9Sstevel@tonic-gate     if (service_name)
8557c478bd9Sstevel@tonic-gate     	free(service_name);
85656a424ccSmp153739     return &ret;
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate generic_ret *
rename_principal_2_svc(rprinc_arg * arg,struct svc_req * rqstp)860159d09a2SMark Phalan rename_principal_2_svc(rprinc_arg *arg, struct svc_req *rqstp)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate     static generic_ret		ret;
8637c478bd9Sstevel@tonic-gate     char			*prime_arg1 = NULL, *prime_arg2 = NULL;
8647c478bd9Sstevel@tonic-gate     char prime_arg[BUFSIZ];
8657c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
8667c478bd9Sstevel@tonic-gate     OM_uint32 min_stat;
8677c478bd9Sstevel@tonic-gate     kadm5_server_handle_t handle;
8687c478bd9Sstevel@tonic-gate     restriction_t *rp;
869159d09a2SMark Phalan     const char                        *errmsg = NULL;
8707c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
871618b6b99Sps57422     size_t tlen1, tlen2, clen, slen;
872618b6b99Sps57422     char *tdots1, *tdots2, *cdots, *sdots;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
8757c478bd9Sstevel@tonic-gate 
87656a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
87756a424ccSmp153739 	 return &ret;
8787c478bd9Sstevel@tonic-gate 
87956a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
8807c478bd9Sstevel@tonic-gate 	 goto error;
8817c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
8827c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
8837c478bd9Sstevel@tonic-gate 	 goto error;
8847c478bd9Sstevel@tonic-gate     }
88556a424ccSmp153739     if (krb5_unparse_name(handle->context, arg->src, &prime_arg1) ||
88656a424ccSmp153739         krb5_unparse_name(handle->context, arg->dest, &prime_arg2)) {
8877c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
8887c478bd9Sstevel@tonic-gate 	 goto error;
8897c478bd9Sstevel@tonic-gate     }
890618b6b99Sps57422     tlen1 = strlen(prime_arg1);
891618b6b99Sps57422     trunc_name(&tlen1, &tdots1);
892618b6b99Sps57422     tlen2 = strlen(prime_arg2);
893618b6b99Sps57422     trunc_name(&tlen2, &tdots2);
894618b6b99Sps57422     clen = strlen(client_name);
895618b6b99Sps57422     trunc_name(&clen, &cdots);
896618b6b99Sps57422     slen = strlen(service_name);
897618b6b99Sps57422     trunc_name(&slen, &sdots);
89856a424ccSmp153739 
899618b6b99Sps57422     (void) snprintf(prime_arg, sizeof (prime_arg), "%.*s%s to %.*s*s",
900618b6b99Sps57422 	tlen1, prime_arg1, tdots1,
901618b6b99Sps57422 	tlen2, prime_arg2, tdots2);
9027c478bd9Sstevel@tonic-gate     ret.code = KADM5_OK;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
9057c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
9067c478bd9Sstevel@tonic-gate 		goto error;
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate     if (! CHANGEPW_SERVICE(rqstp)) {
91056a424ccSmp153739 	 if (!kadm5int_acl_check(handle->context, name,
9117c478bd9Sstevel@tonic-gate 			ACL_DELETE, arg->src, NULL))
9127c478bd9Sstevel@tonic-gate 	      ret.code = KADM5_AUTH_DELETE;
9137c478bd9Sstevel@tonic-gate 	 /* any restrictions at all on the ADD kills the RENAME */
91456a424ccSmp153739 	 if (!kadm5int_acl_check(handle->context, name,
9157c478bd9Sstevel@tonic-gate 			ACL_ADD, arg->dest, &rp)) {
9167c478bd9Sstevel@tonic-gate 	      if (ret.code == KADM5_AUTH_DELETE)
9177c478bd9Sstevel@tonic-gate 		   ret.code = KADM5_AUTH_INSUFFICIENT;
9187c478bd9Sstevel@tonic-gate 	      else
9197c478bd9Sstevel@tonic-gate 		   ret.code = KADM5_AUTH_ADD;
9207c478bd9Sstevel@tonic-gate 	 }
9217c478bd9Sstevel@tonic-gate     } else
9227c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_INSUFFICIENT;
9237c478bd9Sstevel@tonic-gate     if (ret.code != KADM5_OK) {
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
9267c478bd9Sstevel@tonic-gate 				    "kadm5_rename_principal",
9277c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
928618b6b99Sps57422 		krb5_klog_syslog(LOG_NOTICE,
929618b6b99Sps57422 		    "Unauthorized request: kadm5_rename_principal, "
930618b6b99Sps57422 		    "%.*s%s to %.*s%s, "
931618b6b99Sps57422 		    "client=%.*s%s, service=%.*s%s, addr=%s",
932618b6b99Sps57422 		    tlen1, prime_arg1, tdots1,
933618b6b99Sps57422 		    tlen2, prime_arg2, tdots2,
934618b6b99Sps57422 		    clen, client_name, cdots,
935618b6b99Sps57422 		    slen, service_name, sdots,
936618b6b99Sps57422 		    client_addr(rqstp, buf));
9377c478bd9Sstevel@tonic-gate     } else {
9387c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_rename_principal((void *)handle, arg->src,
9397c478bd9Sstevel@tonic-gate 						arg->dest);
940159d09a2SMark Phalan 	/* Solaris Kerberos */
941159d09a2SMark Phalan 	 if( ret.code != 0 )
942159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
9457c478bd9Sstevel@tonic-gate 				"kadm5_rename_principal",
9467c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
947618b6b99Sps57422 	 krb5_klog_syslog(LOG_NOTICE,
948618b6b99Sps57422 			  "Request: kadm5_rename_principal, "
949618b6b99Sps57422 			  "%.*s%s to %.*s%s, %s, "
950618b6b99Sps57422 			  "client=%.*s%s, service=%.*s%s, addr=%s",
951618b6b99Sps57422 			  tlen1, prime_arg1, tdots1,
952618b6b99Sps57422 			  tlen2, prime_arg2, tdots2,
953159d09a2SMark Phalan 			  errmsg ? errmsg : "success",
954618b6b99Sps57422 			  clen, client_name, cdots,
955618b6b99Sps57422 			  slen, service_name, sdots,
956618b6b99Sps57422 			  client_addr(rqstp, buf));
957159d09a2SMark Phalan 
958159d09a2SMark Phalan 	  if (errmsg != NULL)
959159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
9607c478bd9Sstevel@tonic-gate     }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate error:
9637c478bd9Sstevel@tonic-gate     if (name)
9647c478bd9Sstevel@tonic-gate     	gss_release_name(&min_stat, &name);
9657c478bd9Sstevel@tonic-gate     free_server_handle(handle);
9667c478bd9Sstevel@tonic-gate     if (prime_arg1)
9677c478bd9Sstevel@tonic-gate     	free(prime_arg1);
9687c478bd9Sstevel@tonic-gate     if (prime_arg2)
9697c478bd9Sstevel@tonic-gate     	free(prime_arg2);
9707c478bd9Sstevel@tonic-gate     if (client_name)
9717c478bd9Sstevel@tonic-gate     	free(client_name);
9727c478bd9Sstevel@tonic-gate     if (service_name)
9737c478bd9Sstevel@tonic-gate     	free(service_name);
97456a424ccSmp153739     return &ret;
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate gprinc_ret *
get_principal_2_svc(gprinc_arg * arg,struct svc_req * rqstp)978159d09a2SMark Phalan get_principal_2_svc(gprinc_arg *arg, struct svc_req *rqstp)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate     static gprinc_ret		    ret;
9817c478bd9Sstevel@tonic-gate     kadm5_principal_ent_t_v1	    e;
9827c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL, *funcname;
9837c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
9847c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
9857c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
986159d09a2SMark Phalan     const char                            *errmsg = NULL;
9877c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate     xdr_free(xdr_gprinc_ret, (char *) &ret);
9907c478bd9Sstevel@tonic-gate 
99156a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
99256a424ccSmp153739 	 return &ret;
9937c478bd9Sstevel@tonic-gate 
99456a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
9957c478bd9Sstevel@tonic-gate 		goto error;
9967c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate     funcname = handle->api_version == KADM5_API_VERSION_1 ?
9997c478bd9Sstevel@tonic-gate 	 "kadm5_get_principal (V1)" : "kadm5_get_principal";
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
10027c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
10037c478bd9Sstevel@tonic-gate 		goto error;
10047c478bd9Sstevel@tonic-gate     }
10057c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
10067c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
10077c478bd9Sstevel@tonic-gate 		goto error;
10087c478bd9Sstevel@tonic-gate     }
10097c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
10107c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
10117c478bd9Sstevel@tonic-gate 		goto error;
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate     if (! cmp_gss_krb5_name(handle, name, arg->princ) &&
101556a424ccSmp153739 	(CHANGEPW_SERVICE(rqstp) || !kadm5int_acl_check(handle->context,
10167c478bd9Sstevel@tonic-gate 					       name,
10177c478bd9Sstevel@tonic-gate 					       ACL_INQUIRE,
10187c478bd9Sstevel@tonic-gate 					       arg->princ,
10197c478bd9Sstevel@tonic-gate 					       NULL))) {
10207c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_GET;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
10237c478bd9Sstevel@tonic-gate 				    funcname,
10247c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
102546736d35Ssemery 	 log_unauth(funcname, prime_arg, client_name, service_name,
10267c478bd9Sstevel@tonic-gate 		    client_addr(rqstp, buf));
10277c478bd9Sstevel@tonic-gate     } else {
10287c478bd9Sstevel@tonic-gate 	 if (handle->api_version == KADM5_API_VERSION_1) {
10297c478bd9Sstevel@tonic-gate 	      ret.code  = kadm5_get_principal_v1((void *)handle,
10307c478bd9Sstevel@tonic-gate 						 arg->princ, &e);
10317c478bd9Sstevel@tonic-gate 	      if(ret.code == KADM5_OK) {
103256a424ccSmp153739 		   memcpy(&ret.rec, e, sizeof(kadm5_principal_ent_rec_v1));
10337c478bd9Sstevel@tonic-gate 		   free(e);
10347c478bd9Sstevel@tonic-gate 	      }
10357c478bd9Sstevel@tonic-gate 	 } else {
10367c478bd9Sstevel@tonic-gate 	      ret.code  = kadm5_get_principal((void *)handle,
10377c478bd9Sstevel@tonic-gate 					      arg->princ, &ret.rec,
10387c478bd9Sstevel@tonic-gate 					      arg->mask);
10397c478bd9Sstevel@tonic-gate 	 }
10407c478bd9Sstevel@tonic-gate 
1041159d09a2SMark Phalan 	/* Solaris Kerberos */
1042159d09a2SMark Phalan 	 if( ret.code != 0 )
1043159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1044159d09a2SMark Phalan 
10457c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
10467c478bd9Sstevel@tonic-gate 				funcname,
10477c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
1048159d09a2SMark Phalan 	 log_done(funcname, prime_arg, errmsg ? errmsg : "success",
10497c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
1050159d09a2SMark Phalan 
1051159d09a2SMark Phalan 	  if (errmsg != NULL)
1052159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
10537c478bd9Sstevel@tonic-gate     }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate error:
10567c478bd9Sstevel@tonic-gate 	if (name)
10577c478bd9Sstevel@tonic-gate     	gss_release_name(&min_stat, &name);
10587c478bd9Sstevel@tonic-gate     free_server_handle(handle);
10597c478bd9Sstevel@tonic-gate     if (prime_arg)
10607c478bd9Sstevel@tonic-gate     	free(prime_arg);
10617c478bd9Sstevel@tonic-gate     if (client_name)
10627c478bd9Sstevel@tonic-gate     	free(client_name);
10637c478bd9Sstevel@tonic-gate     if (service_name)
10647c478bd9Sstevel@tonic-gate     	free(service_name);
106556a424ccSmp153739     return &ret;
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate gprincs_ret *
get_princs_2_svc(gprincs_arg * arg,struct svc_req * rqstp)1069159d09a2SMark Phalan get_princs_2_svc(gprincs_arg *arg, struct svc_req *rqstp)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate     static gprincs_ret		    ret;
10727c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
10737c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
10747c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
10757c478bd9Sstevel@tonic-gate     kadm5_server_handle_t handle;
10767c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
1077159d09a2SMark Phalan     const char                            *errmsg = NULL;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate     xdr_free(xdr_gprincs_ret, (char *) &ret);
10807c478bd9Sstevel@tonic-gate 
108156a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
108256a424ccSmp153739 	 return &ret;
10837c478bd9Sstevel@tonic-gate 
108456a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
10857c478bd9Sstevel@tonic-gate 		goto error;
10867c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
10897c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
10907c478bd9Sstevel@tonic-gate 		goto error;
10917c478bd9Sstevel@tonic-gate     }
10927c478bd9Sstevel@tonic-gate     prime_arg = arg->exp;
10937c478bd9Sstevel@tonic-gate     if (prime_arg == NULL)
10947c478bd9Sstevel@tonic-gate 	 prime_arg = "*";
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
10977c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
10987c478bd9Sstevel@tonic-gate 		goto error;
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 
110156a424ccSmp153739     if (CHANGEPW_SERVICE(rqstp) || !kadm5int_acl_check(handle->context,
11027c478bd9Sstevel@tonic-gate 					      name,
11037c478bd9Sstevel@tonic-gate 					      ACL_LIST,
11047c478bd9Sstevel@tonic-gate 					      NULL,
11057c478bd9Sstevel@tonic-gate 					      NULL)) {
11067c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_LIST;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
11097c478bd9Sstevel@tonic-gate 				    "kadm5_get_principals",
11107c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
111146736d35Ssemery 	 log_unauth("kadm5_get_principals", prime_arg, client_name,
11127c478bd9Sstevel@tonic-gate 		    service_name, client_addr(rqstp, buf));
11137c478bd9Sstevel@tonic-gate     } else {
11147c478bd9Sstevel@tonic-gate 	 ret.code  = kadm5_get_principals((void *)handle,
11157c478bd9Sstevel@tonic-gate 					       arg->exp, &ret.princs,
11167c478bd9Sstevel@tonic-gate 					       &ret.count);
1117159d09a2SMark Phalan 	/* Solaris Kerberos */
1118159d09a2SMark Phalan 	 if( ret.code != 0 )
1119159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
11227c478bd9Sstevel@tonic-gate 				"kadm5_get_principals",
11237c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
112446736d35Ssemery 	 log_done("kadm5_get_principals", prime_arg,
1125159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
11267c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
1127159d09a2SMark Phalan 
1128159d09a2SMark Phalan 	  if (errmsg != NULL)
1129159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate error:
11337c478bd9Sstevel@tonic-gate 	if (name)
11347c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
11357c478bd9Sstevel@tonic-gate 	free_server_handle(handle);
11367c478bd9Sstevel@tonic-gate 	if (client_name)
11377c478bd9Sstevel@tonic-gate 		free(client_name);
11387c478bd9Sstevel@tonic-gate 	if (service_name)
11397c478bd9Sstevel@tonic-gate 		free(service_name);
11407c478bd9Sstevel@tonic-gate 	return (&ret);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate generic_ret *
chpass_principal_2_svc(chpass_arg * arg,struct svc_req * rqstp)1144159d09a2SMark Phalan chpass_principal_2_svc(chpass_arg *arg, struct svc_req *rqstp)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
11477c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
11487c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
11497c478bd9Sstevel@tonic-gate     OM_uint32 min_stat;
11507c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1151159d09a2SMark Phalan     const char                            *errmsg = NULL;
11527c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
11557c478bd9Sstevel@tonic-gate 
115656a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
115756a424ccSmp153739 	 return &ret;
11587c478bd9Sstevel@tonic-gate 
115956a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
11607c478bd9Sstevel@tonic-gate 		goto error;
11617c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
11647c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
11657c478bd9Sstevel@tonic-gate 		goto error;
11667c478bd9Sstevel@tonic-gate     }
11677c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
11687c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
11697c478bd9Sstevel@tonic-gate 		goto error;
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
11727c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
11737c478bd9Sstevel@tonic-gate 		goto error;
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate     if (cmp_gss_krb5_name(handle, name, arg->princ)) {
117756a424ccSmp153739 	 ret.code = chpass_principal_wrapper_3((void *)handle, arg->princ,
117856a424ccSmp153739 					       FALSE, 0, NULL, arg->pass);
11797c478bd9Sstevel@tonic-gate     } else if (!(CHANGEPW_SERVICE(rqstp)) &&
118056a424ccSmp153739 	       kadm5int_acl_check(handle->context, name,
11817c478bd9Sstevel@tonic-gate 			 ACL_CHANGEPW, arg->princ, NULL)) {
11827c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_chpass_principal((void *)handle, arg->princ,
11837c478bd9Sstevel@tonic-gate 						arg->pass);
11847c478bd9Sstevel@tonic-gate     } else {
11857c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
11867c478bd9Sstevel@tonic-gate 				    "kadm5_chpass_principal",
11877c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
118846736d35Ssemery 	 log_unauth("kadm5_chpass_principal", prime_arg, client_name,
11897c478bd9Sstevel@tonic-gate 		    service_name, client_addr(rqstp, buf));
11907c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_CHANGEPW;
11917c478bd9Sstevel@tonic-gate     }
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate     if(ret.code != KADM5_AUTH_CHANGEPW) {
1194159d09a2SMark Phalan 	/* Solaris Kerberos */
1195159d09a2SMark Phalan 	 if( ret.code != 0 )
1196159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1197159d09a2SMark Phalan 
11987c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
11997c478bd9Sstevel@tonic-gate 				"kadm5_chpass_principal",
12007c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
120146736d35Ssemery 	log_done("kadm5_chpass_principal", prime_arg,
1202159d09a2SMark Phalan 		 errmsg ? errmsg : "success",
12037c478bd9Sstevel@tonic-gate 		 client_name, service_name, client_addr(rqstp, buf));
1204159d09a2SMark Phalan 
1205159d09a2SMark Phalan 	  if (errmsg != NULL)
1206159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
12077c478bd9Sstevel@tonic-gate     }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate error:
12107c478bd9Sstevel@tonic-gate 	if (name)
12117c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
12127c478bd9Sstevel@tonic-gate 	free_server_handle(handle);
12137c478bd9Sstevel@tonic-gate 	if (prime_arg)
12147c478bd9Sstevel@tonic-gate 		free(prime_arg);
12157c478bd9Sstevel@tonic-gate 	if (client_name)
12167c478bd9Sstevel@tonic-gate 		free(client_name);
12177c478bd9Sstevel@tonic-gate 	if (service_name)
12187c478bd9Sstevel@tonic-gate 		free(service_name);
12197c478bd9Sstevel@tonic-gate 	return (&ret);
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate generic_ret *
chpass_principal3_2_svc(chpass3_arg * arg,struct svc_req * rqstp)1223159d09a2SMark Phalan chpass_principal3_2_svc(chpass3_arg *arg, struct svc_req *rqstp)
12247c478bd9Sstevel@tonic-gate {
12257c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
12267c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
12277c478bd9Sstevel@tonic-gate     char       			    *client_name = NULL,
12287c478bd9Sstevel@tonic-gate 				    *service_name = NULL;
12297c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
12307c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1231159d09a2SMark Phalan     const char                            *errmsg = NULL;
12327c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
12357c478bd9Sstevel@tonic-gate 
123656a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
12377c478bd9Sstevel@tonic-gate 	 return &ret;
12387c478bd9Sstevel@tonic-gate 
123956a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
12407c478bd9Sstevel@tonic-gate 	goto error;
12417c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
12447c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
12457c478bd9Sstevel@tonic-gate 	goto error;
12467c478bd9Sstevel@tonic-gate     }
12477c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
12487c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
12497c478bd9Sstevel@tonic-gate 	goto error;
12507c478bd9Sstevel@tonic-gate     }
12517c478bd9Sstevel@tonic-gate     if (!(name = get_clnt_name(rqstp))) {
12527c478bd9Sstevel@tonic-gate 	ret.code = KADM5_FAILURE;
12537c478bd9Sstevel@tonic-gate 	goto error;
12547c478bd9Sstevel@tonic-gate     }
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate     if (cmp_gss_krb5_name(handle, name, arg->princ)) {
125756a424ccSmp153739 	 ret.code = chpass_principal_wrapper_3((void *)handle, arg->princ,
125856a424ccSmp153739 					       arg->keepold,
125956a424ccSmp153739 					       arg->n_ks_tuple,
126056a424ccSmp153739 					       arg->ks_tuple,
12617c478bd9Sstevel@tonic-gate 					       arg->pass);
12627c478bd9Sstevel@tonic-gate     } else if (!(CHANGEPW_SERVICE(rqstp)) &&
126356a424ccSmp153739 	       kadm5int_acl_check(handle->context, name,
12647c478bd9Sstevel@tonic-gate 			 ACL_CHANGEPW, arg->princ, NULL)) {
12657c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_chpass_principal_3((void *)handle, arg->princ,
12667c478bd9Sstevel@tonic-gate 					     arg->keepold,
12677c478bd9Sstevel@tonic-gate 					     arg->n_ks_tuple,
12687c478bd9Sstevel@tonic-gate 					     arg->ks_tuple,
12697c478bd9Sstevel@tonic-gate 					     arg->pass);
12707c478bd9Sstevel@tonic-gate     } else {
127146736d35Ssemery 	 log_unauth("kadm5_chpass_principal", prime_arg,
127246736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
12737c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_CHANGEPW;
12747c478bd9Sstevel@tonic-gate     }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate     if(ret.code != KADM5_AUTH_CHANGEPW) {
1277159d09a2SMark Phalan 	/* Solaris Kerberos */
1278159d09a2SMark Phalan 	if( ret.code != 0 )
1279159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1280159d09a2SMark Phalan 
128146736d35Ssemery 	log_done("kadm5_chpass_principal", prime_arg,
1282159d09a2SMark Phalan 		errmsg ? errmsg : "success",
128346736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
1284159d09a2SMark Phalan 
1285159d09a2SMark Phalan 	  if (errmsg != NULL)
1286159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
12877c478bd9Sstevel@tonic-gate     }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate error:
12907c478bd9Sstevel@tonic-gate     if (name)
12917c478bd9Sstevel@tonic-gate     	gss_release_name(&min_stat, &name);
12927c478bd9Sstevel@tonic-gate     free_server_handle(handle);
12937c478bd9Sstevel@tonic-gate     if (client_name)
12947c478bd9Sstevel@tonic-gate     	free(client_name);
12957c478bd9Sstevel@tonic-gate     if (service_name)
12967c478bd9Sstevel@tonic-gate     	free(service_name);
12977c478bd9Sstevel@tonic-gate     if (prime_arg)
12987c478bd9Sstevel@tonic-gate     	free(prime_arg);
12997c478bd9Sstevel@tonic-gate     return (&ret);
13007c478bd9Sstevel@tonic-gate }
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate #ifdef SUNWOFF
13037c478bd9Sstevel@tonic-gate generic_ret *
setv4key_principal_2_svc(setv4key_arg * arg,struct svc_req * rqstp)1304159d09a2SMark Phalan setv4key_principal_2_svc(setv4key_arg *arg, struct svc_req *rqstp)
13057c478bd9Sstevel@tonic-gate {
13067c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
13077c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
13087c478bd9Sstevel@tonic-gate     char 			    *client_name = NULL,
13097c478bd9Sstevel@tonic-gate 				    *service_name = NULL;
13107c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
13117c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1312159d09a2SMark Phalan     const char                            *errmsg = NULL;
13137c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
13167c478bd9Sstevel@tonic-gate 
131756a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
13187c478bd9Sstevel@tonic-gate 	 return &ret;
13197c478bd9Sstevel@tonic-gate 
132056a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
13217c478bd9Sstevel@tonic-gate 	goto error;
13227c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
13257c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
13267c478bd9Sstevel@tonic-gate 	goto error;
13277c478bd9Sstevel@tonic-gate     }
13287c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
13297c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
13307c478bd9Sstevel@tonic-gate 	goto error;
13317c478bd9Sstevel@tonic-gate     }
13327c478bd9Sstevel@tonic-gate     if (!(name = get_clnt_name(rqstp))) {
13337c478bd9Sstevel@tonic-gate 	ret.code = KADM5_FAILURE;
13347c478bd9Sstevel@tonic-gate 	goto error;
13357c478bd9Sstevel@tonic-gate     }
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate     if (!(CHANGEPW_SERVICE(rqstp)) &&
133856a424ccSmp153739 	       kadm5int_acl_check(handle->context, name,
133956a424ccSmp153739 			 ACL_SETKEY, arg->princ, NULL)) {
13407c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_setv4key_principal((void *)handle, arg->princ,
13417c478bd9Sstevel@tonic-gate 					     arg->keyblock);
13427c478bd9Sstevel@tonic-gate     } else {
134346736d35Ssemery 	 log_unauth("kadm5_setv4key_principal", prime_arg,
134446736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
13457c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_SETKEY;
13467c478bd9Sstevel@tonic-gate     }
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate     if(ret.code != KADM5_AUTH_SETKEY) {
1349159d09a2SMark Phalan 	/* Solaris Kerberos */
1350159d09a2SMark Phalan 	if( ret.code != 0 )
1351159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1352159d09a2SMark Phalan 
135346736d35Ssemery 	log_done("kadm5_setv4key_principal", prime_arg,
1354159d09a2SMark Phalan 		 errmsg ? errmsg : "success",
135546736d35Ssemery 		 client_name, service_name, client_addr(rqstp, buf));
1356159d09a2SMark Phalan 
1357159d09a2SMark Phalan 	  if (errmsg != NULL)
1358159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
13597c478bd9Sstevel@tonic-gate     }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate error:
13627c478bd9Sstevel@tonic-gate     if (name)
13637c478bd9Sstevel@tonic-gate 	gss_release_name(&min_stat, &name);
13647c478bd9Sstevel@tonic-gate     free_server_handle(handle);
13657c478bd9Sstevel@tonic-gate     if (client_name)
13667c478bd9Sstevel@tonic-gate 	free(client_name);
13677c478bd9Sstevel@tonic-gate     if (service_name)
13687c478bd9Sstevel@tonic-gate 	free(service_name);
13697c478bd9Sstevel@tonic-gate     if (prime_arg)
13707c478bd9Sstevel@tonic-gate 	free(prime_arg);
13717c478bd9Sstevel@tonic-gate     return (&ret);
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate #endif
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate generic_ret *
setkey_principal_2_svc(setkey_arg * arg,struct svc_req * rqstp)1376159d09a2SMark Phalan setkey_principal_2_svc(setkey_arg *arg, struct svc_req *rqstp)
13777c478bd9Sstevel@tonic-gate {
13787c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
13797c478bd9Sstevel@tonic-gate     char			    *prime_arg;
13807c478bd9Sstevel@tonic-gate     char			    *client_name,
13817c478bd9Sstevel@tonic-gate 				    *service_name;
13827c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
13837c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1384159d09a2SMark Phalan     const char                            *errmsg = NULL;
13857c478bd9Sstevel@tonic-gate     gss_name_t name;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
13887c478bd9Sstevel@tonic-gate 
138956a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
13907c478bd9Sstevel@tonic-gate 	 return &ret;
13917c478bd9Sstevel@tonic-gate 
139256a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
13937c478bd9Sstevel@tonic-gate 	goto error;
13947c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
13977c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
13987c478bd9Sstevel@tonic-gate 	goto error;
13997c478bd9Sstevel@tonic-gate     }
14007c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
14017c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
14027c478bd9Sstevel@tonic-gate 	goto error;
14037c478bd9Sstevel@tonic-gate     }
14047c478bd9Sstevel@tonic-gate     if (!(name = get_clnt_name(rqstp))) {
14057c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
14067c478bd9Sstevel@tonic-gate 	goto error;
14077c478bd9Sstevel@tonic-gate     }
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate     if (!(CHANGEPW_SERVICE(rqstp)) &&
141056a424ccSmp153739 	       kadm5int_acl_check(handle->context, name, ACL_SETKEY, arg->princ, NULL)) {
14117c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_setkey_principal((void *)handle, arg->princ,
14127c478bd9Sstevel@tonic-gate 					   arg->keyblocks, arg->n_keys);
14137c478bd9Sstevel@tonic-gate     } else {
141446736d35Ssemery 	 log_unauth("kadm5_setkey_principal", prime_arg,
141546736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
14167c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_SETKEY;
14177c478bd9Sstevel@tonic-gate     }
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate     if(ret.code != KADM5_AUTH_SETKEY) {
1420159d09a2SMark Phalan 	/* Solaris Kerberos */
1421159d09a2SMark Phalan 	if( ret.code != 0 )
1422159d09a2SMark Phalan 	    errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1423159d09a2SMark Phalan 
142446736d35Ssemery 	log_done("kadm5_setkey_principal", prime_arg,
1425159d09a2SMark Phalan 		 errmsg ? errmsg : "success",
142646736d35Ssemery 		 client_name, service_name, client_addr(rqstp, buf));
1427159d09a2SMark Phalan 
1428159d09a2SMark Phalan 	  if (errmsg != NULL)
1429159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
14307c478bd9Sstevel@tonic-gate     }
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate error:
14337c478bd9Sstevel@tonic-gate     if (name)
14347c478bd9Sstevel@tonic-gate 	gss_release_name(&min_stat, &name);
14357c478bd9Sstevel@tonic-gate     free_server_handle(handle);
14367c478bd9Sstevel@tonic-gate     if (client_name)
14377c478bd9Sstevel@tonic-gate     	free(client_name);
14387c478bd9Sstevel@tonic-gate     if (service_name)
14397c478bd9Sstevel@tonic-gate     	free(service_name);
14407c478bd9Sstevel@tonic-gate     if (prime_arg)
14417c478bd9Sstevel@tonic-gate     	free(prime_arg);
14427c478bd9Sstevel@tonic-gate     return (&ret);
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate generic_ret *
setkey_principal3_2_svc(setkey3_arg * arg,struct svc_req * rqstp)1446159d09a2SMark Phalan setkey_principal3_2_svc(setkey3_arg *arg, struct svc_req *rqstp)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
14497c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
14507c478bd9Sstevel@tonic-gate     char			    *client_name = NULL,
14517c478bd9Sstevel@tonic-gate 				    *service_name = NULL;
14527c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
14537c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1454159d09a2SMark Phalan     const char                            *errmsg = NULL;
14557c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
14587c478bd9Sstevel@tonic-gate 
145956a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
14607c478bd9Sstevel@tonic-gate 	 return &ret;
14617c478bd9Sstevel@tonic-gate 
146256a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
14637c478bd9Sstevel@tonic-gate 	goto error;
14647c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
14677c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
14687c478bd9Sstevel@tonic-gate 	goto error;
14697c478bd9Sstevel@tonic-gate     }
14707c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
14717c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
14727c478bd9Sstevel@tonic-gate 	goto error;
14737c478bd9Sstevel@tonic-gate     }
14747c478bd9Sstevel@tonic-gate     if (!(name = get_clnt_name(rqstp))) {
14757c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
14767c478bd9Sstevel@tonic-gate 	goto error;
14777c478bd9Sstevel@tonic-gate     }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate     if (!(CHANGEPW_SERVICE(rqstp)) &&
148056a424ccSmp153739 	       kadm5int_acl_check(handle->context, name,
148156a424ccSmp153739 			 ACL_SETKEY, arg->princ, NULL)) {
14827c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_setkey_principal_3((void *)handle, arg->princ,
14837c478bd9Sstevel@tonic-gate 					     arg->keepold,
14847c478bd9Sstevel@tonic-gate 					     arg->n_ks_tuple,
14857c478bd9Sstevel@tonic-gate 					     arg->ks_tuple,
14867c478bd9Sstevel@tonic-gate 					     arg->keyblocks, arg->n_keys);
14877c478bd9Sstevel@tonic-gate     } else {
148846736d35Ssemery 	 log_unauth("kadm5_setkey_principal", prime_arg,
148946736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
14907c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_SETKEY;
14917c478bd9Sstevel@tonic-gate     }
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate     if(ret.code != KADM5_AUTH_SETKEY) {
1494159d09a2SMark Phalan 	/* Solaris Kerberos */
1495159d09a2SMark Phalan 	if( ret.code != 0 )
1496159d09a2SMark Phalan 	    errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1497159d09a2SMark Phalan 
149846736d35Ssemery 	log_done("kadm5_setkey_principal", prime_arg,
1499159d09a2SMark Phalan 		 errmsg ? errmsg : "success",
150046736d35Ssemery 		 client_name, service_name, client_addr(rqstp, buf));
1501159d09a2SMark Phalan 
1502159d09a2SMark Phalan 	  if (errmsg != NULL)
1503159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
15047c478bd9Sstevel@tonic-gate     }
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate error:
15077c478bd9Sstevel@tonic-gate     if (name)
15087c478bd9Sstevel@tonic-gate 	gss_release_name(&min_stat, &name);
15097c478bd9Sstevel@tonic-gate     free_server_handle(handle);
15107c478bd9Sstevel@tonic-gate     if (client_name)
15117c478bd9Sstevel@tonic-gate 	free(client_name);
15127c478bd9Sstevel@tonic-gate     if (service_name)
15137c478bd9Sstevel@tonic-gate     	free(service_name);
15147c478bd9Sstevel@tonic-gate     if (prime_arg)
15157c478bd9Sstevel@tonic-gate     	free(prime_arg);
151656a424ccSmp153739     return &ret;
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate chrand_ret *
chrand_principal_2_svc(chrand_arg * arg,struct svc_req * rqstp)1520159d09a2SMark Phalan chrand_principal_2_svc(chrand_arg *arg, struct svc_req *rqstp)
15217c478bd9Sstevel@tonic-gate {
15227c478bd9Sstevel@tonic-gate     static chrand_ret		ret;
15237c478bd9Sstevel@tonic-gate     krb5_keyblock		*k;
15247c478bd9Sstevel@tonic-gate     int				nkeys;
15257c478bd9Sstevel@tonic-gate     char			*prime_arg = NULL, *funcname;
15267c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
15277c478bd9Sstevel@tonic-gate     OM_uint32			min_stat;
15287c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	handle;
1529159d09a2SMark Phalan     const char                        *errmsg = NULL;
15307c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate     xdr_free(xdr_chrand_ret, (char *) &ret);
15337c478bd9Sstevel@tonic-gate 
153456a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
153556a424ccSmp153739 	 return &ret;
15367c478bd9Sstevel@tonic-gate 
153756a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
15387c478bd9Sstevel@tonic-gate 		goto error;
153956a424ccSmp153739 
15407c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate     funcname = handle->api_version == KADM5_API_VERSION_1 ?
15437c478bd9Sstevel@tonic-gate 	 "kadm5_randkey_principal (V1)" : "kadm5_randkey_principal";
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
15467c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
15477c478bd9Sstevel@tonic-gate 		goto error;
15487c478bd9Sstevel@tonic-gate     }
15497c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
15507c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
15517c478bd9Sstevel@tonic-gate 		goto error;
15527c478bd9Sstevel@tonic-gate     }
15537c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
15547c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
15557c478bd9Sstevel@tonic-gate 		goto error;
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate     if (cmp_gss_krb5_name(handle, name, arg->princ)) {
15593125ebfcSsemery 	 ret.code = randkey_principal_wrapper((void *)handle, arg->princ, &k,
15603125ebfcSsemery 						&nkeys);
15617c478bd9Sstevel@tonic-gate     } else if (!(CHANGEPW_SERVICE(rqstp)) &&
156256a424ccSmp153739 	       kadm5int_acl_check(handle->context, name,
15637c478bd9Sstevel@tonic-gate 			 ACL_CHANGEPW, arg->princ, NULL)) {
15647c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_randkey_principal((void *)handle, arg->princ,
15657c478bd9Sstevel@tonic-gate 					    &k, &nkeys);
15667c478bd9Sstevel@tonic-gate     } else {
15677c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
15687c478bd9Sstevel@tonic-gate 				    funcname, prime_arg, client_name);
156946736d35Ssemery 	 log_unauth(funcname, prime_arg,
157046736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
15717c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_CHANGEPW;
15727c478bd9Sstevel@tonic-gate     }
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate     if(ret.code == KADM5_OK) {
15757c478bd9Sstevel@tonic-gate 	 if (handle->api_version == KADM5_API_VERSION_1) {
157656a424ccSmp153739 	      krb5_copy_keyblock_contents(handle->context, k, &ret.key);
15777c478bd9Sstevel@tonic-gate 	      krb5_free_keyblock(handle->context, k);
15787c478bd9Sstevel@tonic-gate 	 } else {
15797c478bd9Sstevel@tonic-gate 	      ret.keys = k;
15807c478bd9Sstevel@tonic-gate 	      ret.n_keys = nkeys;
15817c478bd9Sstevel@tonic-gate 	 }
15827c478bd9Sstevel@tonic-gate     }
158356a424ccSmp153739 
15847c478bd9Sstevel@tonic-gate     if(ret.code != KADM5_AUTH_CHANGEPW) {
1585159d09a2SMark Phalan 	/* Solaris Kerberos */
1586159d09a2SMark Phalan 	if( ret.code != 0 )
1587159d09a2SMark Phalan 	    errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1588159d09a2SMark Phalan 
15897c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
15907c478bd9Sstevel@tonic-gate 				funcname, prime_arg, client_name, ret.code);
1591159d09a2SMark Phalan 	log_done(funcname, prime_arg, errmsg ? errmsg : "success",
15927c478bd9Sstevel@tonic-gate 		 client_name, service_name, client_addr(rqstp, buf));
1593159d09a2SMark Phalan 
1594159d09a2SMark Phalan 	  if (errmsg != NULL)
1595159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
15967c478bd9Sstevel@tonic-gate     }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate error:
15997c478bd9Sstevel@tonic-gate 	if (name)
16007c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
16017c478bd9Sstevel@tonic-gate 	free_server_handle(handle);
16027c478bd9Sstevel@tonic-gate 	if (prime_arg)
16037c478bd9Sstevel@tonic-gate     	free(prime_arg);
16047c478bd9Sstevel@tonic-gate     if (client_name)
16057c478bd9Sstevel@tonic-gate     	free(client_name);
16067c478bd9Sstevel@tonic-gate     if (service_name)
16077c478bd9Sstevel@tonic-gate     	free(service_name);
160856a424ccSmp153739     return &ret;
16097c478bd9Sstevel@tonic-gate }
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate chrand_ret *
chrand_principal3_2_svc(chrand3_arg * arg,struct svc_req * rqstp)1612159d09a2SMark Phalan chrand_principal3_2_svc(chrand3_arg *arg, struct svc_req *rqstp)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate     static chrand_ret		ret;
16157c478bd9Sstevel@tonic-gate     krb5_keyblock		*k;
16167c478bd9Sstevel@tonic-gate     int				nkeys;
16177c478bd9Sstevel@tonic-gate     char			*prime_arg = NULL, *funcname;
16187c478bd9Sstevel@tonic-gate     char			*client_name = NULL,
16197c478bd9Sstevel@tonic-gate 	    			*service_name = NULL;
16207c478bd9Sstevel@tonic-gate     OM_uint32			min_stat;
16217c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	handle;
1622159d09a2SMark Phalan     const char                        *errmsg = NULL;
16237c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate     xdr_free(xdr_chrand_ret, (char *) &ret);
16267c478bd9Sstevel@tonic-gate 
162756a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
16287c478bd9Sstevel@tonic-gate 	 return &ret;
16297c478bd9Sstevel@tonic-gate 
163056a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
16317c478bd9Sstevel@tonic-gate 	goto error;
16327c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate     funcname = handle->api_version == KADM5_API_VERSION_1 ?
16357c478bd9Sstevel@tonic-gate 	 "kadm5_randkey_principal (V1)" : "kadm5_randkey_principal";
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
16387c478bd9Sstevel@tonic-gate 	ret.code = KADM5_FAILURE;
16397c478bd9Sstevel@tonic-gate 	goto error;
16407c478bd9Sstevel@tonic-gate     }
16417c478bd9Sstevel@tonic-gate     if (krb5_unparse_name(handle->context, arg->princ, &prime_arg)) {
16427c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_BAD_PRINCIPAL;
16437c478bd9Sstevel@tonic-gate 	goto error;
16447c478bd9Sstevel@tonic-gate     }
16457c478bd9Sstevel@tonic-gate     if (!(name = get_clnt_name(rqstp))) {
16467c478bd9Sstevel@tonic-gate 	ret.code = KADM5_FAILURE;
16477c478bd9Sstevel@tonic-gate 	goto error;
16487c478bd9Sstevel@tonic-gate     }
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate     if (cmp_gss_krb5_name(handle, name, arg->princ)) {
165156a424ccSmp153739 	 ret.code = randkey_principal_wrapper_3((void *)handle, arg->princ,
165256a424ccSmp153739 						arg->keepold,
165356a424ccSmp153739 						arg->n_ks_tuple,
165456a424ccSmp153739 						arg->ks_tuple,
165556a424ccSmp153739 						&k, &nkeys);
16567c478bd9Sstevel@tonic-gate     } else if (!(CHANGEPW_SERVICE(rqstp)) &&
165756a424ccSmp153739 	       kadm5int_acl_check(handle->context, name,
16587c478bd9Sstevel@tonic-gate 			 ACL_CHANGEPW, arg->princ, NULL)) {
16597c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_randkey_principal_3((void *)handle, arg->princ,
16607c478bd9Sstevel@tonic-gate 					      arg->keepold,
16617c478bd9Sstevel@tonic-gate 					      arg->n_ks_tuple,
16627c478bd9Sstevel@tonic-gate 					      arg->ks_tuple,
16637c478bd9Sstevel@tonic-gate 					      &k, &nkeys);
16647c478bd9Sstevel@tonic-gate     } else {
166546736d35Ssemery 	 log_unauth(funcname, prime_arg,
166646736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
16677c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_CHANGEPW;
16687c478bd9Sstevel@tonic-gate     }
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate     if(ret.code == KADM5_OK) {
16717c478bd9Sstevel@tonic-gate 	 if (handle->api_version == KADM5_API_VERSION_1) {
16727c478bd9Sstevel@tonic-gate 	      krb5_copy_keyblock_contents(handle->context, k, &ret.key);
16737c478bd9Sstevel@tonic-gate 	      krb5_free_keyblock(handle->context, k);
16747c478bd9Sstevel@tonic-gate 	 } else {
16757c478bd9Sstevel@tonic-gate 	      ret.keys = k;
16767c478bd9Sstevel@tonic-gate 	      ret.n_keys = nkeys;
16777c478bd9Sstevel@tonic-gate 	 }
16787c478bd9Sstevel@tonic-gate     }
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate     if(ret.code != KADM5_AUTH_CHANGEPW) {
1681159d09a2SMark Phalan 	/* Solaris Kerberos */
1682159d09a2SMark Phalan 	if( ret.code != 0 )
1683159d09a2SMark Phalan 	    errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1684159d09a2SMark Phalan 
1685159d09a2SMark Phalan 	log_done(funcname, prime_arg, errmsg ? errmsg : "success",
168646736d35Ssemery 		 client_name, service_name, client_addr(rqstp, buf));
1687159d09a2SMark Phalan 
1688159d09a2SMark Phalan 	if (errmsg != NULL)
1689159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
16907c478bd9Sstevel@tonic-gate     }
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate error:
16937c478bd9Sstevel@tonic-gate     if (name)
16947c478bd9Sstevel@tonic-gate 	gss_release_name(&min_stat, &name);
16957c478bd9Sstevel@tonic-gate     free_server_handle(handle);
16967c478bd9Sstevel@tonic-gate     if (client_name)
16977c478bd9Sstevel@tonic-gate 	free(client_name);
16987c478bd9Sstevel@tonic-gate     if (service_name)
16997c478bd9Sstevel@tonic-gate 	free(service_name);
17007c478bd9Sstevel@tonic-gate     if (prime_arg)
17017c478bd9Sstevel@tonic-gate 	free(prime_arg);
17027c478bd9Sstevel@tonic-gate     return (&ret);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate generic_ret *
create_policy_2_svc(cpol_arg * arg,struct svc_req * rqstp)1706159d09a2SMark Phalan create_policy_2_svc(cpol_arg *arg, struct svc_req *rqstp)
17077c478bd9Sstevel@tonic-gate {
17087c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
17097c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
17107c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
17117c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
17127c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1713159d09a2SMark Phalan     const char                            *errmsg = NULL;
17147c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
17177c478bd9Sstevel@tonic-gate 
171856a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
171956a424ccSmp153739 	 return &ret;
17207c478bd9Sstevel@tonic-gate 
172156a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
17227c478bd9Sstevel@tonic-gate 		goto error;
172356a424ccSmp153739 
17247c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
17277c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
17287c478bd9Sstevel@tonic-gate 		goto error;
17297c478bd9Sstevel@tonic-gate     }
17307c478bd9Sstevel@tonic-gate     prime_arg = arg->rec.policy;
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
17337c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
17347c478bd9Sstevel@tonic-gate 		goto error;
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
173756a424ccSmp153739     if (CHANGEPW_SERVICE(rqstp) || !kadm5int_acl_check(handle->context,
17387c478bd9Sstevel@tonic-gate 					      name,
17397c478bd9Sstevel@tonic-gate 					      ACL_ADD, NULL, NULL)) {
17407c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_ADD;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
17437c478bd9Sstevel@tonic-gate 				    "kadm5_create_policy",
17447c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
174546736d35Ssemery 	 log_unauth("kadm5_create_policy", prime_arg,
174646736d35Ssemery 		 client_name, service_name, client_addr(rqstp, buf));
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate     } else {
17497c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_create_policy((void *)handle, &arg->rec,
17507c478bd9Sstevel@tonic-gate 					     arg->mask);
1751159d09a2SMark Phalan 	/* Solaris Kerberos */
1752159d09a2SMark Phalan 	 if( ret.code != 0 )
1753159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
17567c478bd9Sstevel@tonic-gate 				"kadm5_create_policy",
17577c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
175846736d35Ssemery 	 log_done("kadm5_create_policy",
17597c478bd9Sstevel@tonic-gate 		  ((prime_arg == NULL) ? "(null)" : prime_arg),
1760159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
17617c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
1762159d09a2SMark Phalan 
1763159d09a2SMark Phalan 	  if (errmsg != NULL)
1764159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
17657c478bd9Sstevel@tonic-gate     }
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate error:
17687c478bd9Sstevel@tonic-gate 	if (name)
17697c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
17707c478bd9Sstevel@tonic-gate     free_server_handle(handle);
17717c478bd9Sstevel@tonic-gate     if (client_name)
17727c478bd9Sstevel@tonic-gate     	free(client_name);
17737c478bd9Sstevel@tonic-gate     if (service_name)
17747c478bd9Sstevel@tonic-gate     	free(service_name);
177556a424ccSmp153739     return &ret;
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate generic_ret *
delete_policy_2_svc(dpol_arg * arg,struct svc_req * rqstp)1779159d09a2SMark Phalan delete_policy_2_svc(dpol_arg *arg, struct svc_req *rqstp)
17807c478bd9Sstevel@tonic-gate {
17817c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
17827c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
17837c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
17847c478bd9Sstevel@tonic-gate     OM_uint32			    min_stat;
17857c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1786159d09a2SMark Phalan     const char                            *errmsg = NULL;
17877c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
17907c478bd9Sstevel@tonic-gate 
179156a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
179256a424ccSmp153739 	 return &ret;
17937c478bd9Sstevel@tonic-gate 
179456a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
17957c478bd9Sstevel@tonic-gate 		goto error;
17967c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
17997c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
18007c478bd9Sstevel@tonic-gate 		goto error;
18017c478bd9Sstevel@tonic-gate     }
18027c478bd9Sstevel@tonic-gate     prime_arg = arg->name;
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
18057c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
18067c478bd9Sstevel@tonic-gate 		goto error;
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
180956a424ccSmp153739     if (CHANGEPW_SERVICE(rqstp) || !kadm5int_acl_check(handle->context,
18107c478bd9Sstevel@tonic-gate 						name,
18117c478bd9Sstevel@tonic-gate 					      ACL_DELETE, NULL, NULL)) {
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
18147c478bd9Sstevel@tonic-gate 				    "kadm5_delete_policy",
18157c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
181646736d35Ssemery 	 log_unauth("kadm5_delete_policy", prime_arg,
181746736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
18187c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_DELETE;
18197c478bd9Sstevel@tonic-gate     } else {
18207c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_delete_policy((void *)handle, arg->name);
1821159d09a2SMark Phalan 	/* Solaris Kerberos */
1822159d09a2SMark Phalan 	 if( ret.code != 0 )
1823159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
18267c478bd9Sstevel@tonic-gate 				"kadm5_delete_policy",
18277c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
182846736d35Ssemery 	 log_done("kadm5_delete_policy",
18297c478bd9Sstevel@tonic-gate 		  ((prime_arg == NULL) ? "(null)" : prime_arg),
1830159d09a2SMark Phalan 		 errmsg ? errmsg : "success",
18317c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
1832159d09a2SMark Phalan 
1833159d09a2SMark Phalan 	 if (errmsg != NULL)
1834159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
18357c478bd9Sstevel@tonic-gate     }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate error:
18387c478bd9Sstevel@tonic-gate 	if (name)
18397c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
18407c478bd9Sstevel@tonic-gate     free_server_handle(handle);
18417c478bd9Sstevel@tonic-gate     if (client_name)
18427c478bd9Sstevel@tonic-gate     free(client_name);
18437c478bd9Sstevel@tonic-gate     if (service_name)
18447c478bd9Sstevel@tonic-gate     free(service_name);
184556a424ccSmp153739     return &ret;
18467c478bd9Sstevel@tonic-gate }
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate generic_ret *
modify_policy_2_svc(mpol_arg * arg,struct svc_req * rqstp)1849159d09a2SMark Phalan modify_policy_2_svc(mpol_arg *arg, struct svc_req *rqstp)
18507c478bd9Sstevel@tonic-gate {
18517c478bd9Sstevel@tonic-gate     static generic_ret		    ret;
18527c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
18537c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
18547c478bd9Sstevel@tonic-gate     OM_uint32 min_stat;
18557c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	    handle;
1856159d09a2SMark Phalan     const char                            *errmsg = NULL;
18577c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate     xdr_free(xdr_generic_ret, (char *) &ret);
18607c478bd9Sstevel@tonic-gate 
186156a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
186256a424ccSmp153739 	 return &ret;
18637c478bd9Sstevel@tonic-gate 
186456a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
18657c478bd9Sstevel@tonic-gate 		goto error;
18667c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
18697c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
18707c478bd9Sstevel@tonic-gate 		goto error;
18717c478bd9Sstevel@tonic-gate     }
18727c478bd9Sstevel@tonic-gate     prime_arg = arg->rec.policy;
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate     if (!(name = get_clnt_name(rqstp))) {
18757c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
18767c478bd9Sstevel@tonic-gate 		goto error;
18777c478bd9Sstevel@tonic-gate     }
18787c478bd9Sstevel@tonic-gate 
187956a424ccSmp153739     if (CHANGEPW_SERVICE(rqstp) || !kadm5int_acl_check(handle->context,
18807c478bd9Sstevel@tonic-gate 						name,
18817c478bd9Sstevel@tonic-gate 					      ACL_MODIFY, NULL, NULL)) {
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
18847c478bd9Sstevel@tonic-gate 				    "kadm5_modify_policy",
18857c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
188646736d35Ssemery 	 log_unauth("kadm5_modify_policy", prime_arg,
188746736d35Ssemery 		client_name, service_name, client_addr(rqstp, buf));
18887c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_MODIFY;
18897c478bd9Sstevel@tonic-gate     } else {
18907c478bd9Sstevel@tonic-gate 	 ret.code = kadm5_modify_policy((void *)handle, &arg->rec,
18917c478bd9Sstevel@tonic-gate 					     arg->mask);
1892159d09a2SMark Phalan 	/* Solaris Kerberos */
1893159d09a2SMark Phalan 	 if( ret.code != 0 )
1894159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
18977c478bd9Sstevel@tonic-gate 				"kadm5_modify_policy",
18987c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
189946736d35Ssemery 	 log_done("kadm5_modify_policy",
19007c478bd9Sstevel@tonic-gate 		  ((prime_arg == NULL) ? "(null)" : prime_arg),
1901159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
19027c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
1903159d09a2SMark Phalan 
1904159d09a2SMark Phalan 	  if (errmsg != NULL)
1905159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
19067c478bd9Sstevel@tonic-gate     }
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate error:
19097c478bd9Sstevel@tonic-gate 	if (name)
19107c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
19117c478bd9Sstevel@tonic-gate 	free_server_handle(handle);
19127c478bd9Sstevel@tonic-gate 	if (client_name)
19137c478bd9Sstevel@tonic-gate 		free(client_name);
19147c478bd9Sstevel@tonic-gate 	if (service_name)
19157c478bd9Sstevel@tonic-gate 		free(service_name);
19167c478bd9Sstevel@tonic-gate 	return (&ret);
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate gpol_ret *
get_policy_2_svc(gpol_arg * arg,struct svc_req * rqstp)1920159d09a2SMark Phalan get_policy_2_svc(gpol_arg *arg, struct svc_req *rqstp)
19217c478bd9Sstevel@tonic-gate {
19227c478bd9Sstevel@tonic-gate     static gpol_ret		ret;
19237c478bd9Sstevel@tonic-gate     kadm5_ret_t		ret2;
19247c478bd9Sstevel@tonic-gate     char *prime_arg = NULL, *funcname;
19257c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
19267c478bd9Sstevel@tonic-gate     OM_uint32 min_stat;
19277c478bd9Sstevel@tonic-gate     kadm5_policy_ent_t	e;
19287c478bd9Sstevel@tonic-gate     kadm5_principal_ent_rec	caller_ent;
19297c478bd9Sstevel@tonic-gate     krb5_principal caller;
19307c478bd9Sstevel@tonic-gate     kadm5_server_handle_t	handle;
1931159d09a2SMark Phalan     const char                        *errmsg = NULL;
19327c478bd9Sstevel@tonic-gate   gss_name_t name = NULL;
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate     xdr_free(xdr_gpol_ret, (char *) &ret);
19357c478bd9Sstevel@tonic-gate 
193656a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
193756a424ccSmp153739 	 return &ret;
19387c478bd9Sstevel@tonic-gate 
193956a424ccSmp153739     if ((ret.code = check_handle((void *) handle)))
19407c478bd9Sstevel@tonic-gate 		goto error;
194156a424ccSmp153739 
19427c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate     funcname = handle->api_version == KADM5_API_VERSION_1 ?
19457c478bd9Sstevel@tonic-gate 	 "kadm5_get_policy (V1)" : "kadm5_get_policy";
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
19487c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
19497c478bd9Sstevel@tonic-gate 		goto error;
19507c478bd9Sstevel@tonic-gate     }
19517c478bd9Sstevel@tonic-gate     prime_arg = arg->name;
19527c478bd9Sstevel@tonic-gate 	ret.code = KADM5_AUTH_GET;
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
19557c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
19567c478bd9Sstevel@tonic-gate 		goto error;
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
195956a424ccSmp153739     if (!CHANGEPW_SERVICE(rqstp) && kadm5int_acl_check(handle->context,
19607c478bd9Sstevel@tonic-gate 						name,
19617c478bd9Sstevel@tonic-gate 						ACL_INQUIRE, NULL, NULL))
19627c478bd9Sstevel@tonic-gate 		ret.code = KADM5_OK;
19637c478bd9Sstevel@tonic-gate 	else {
19647c478bd9Sstevel@tonic-gate 		ret.code = kadm5_get_principal(handle->lhandle,
19657c478bd9Sstevel@tonic-gate 		    handle->current_caller,
19667c478bd9Sstevel@tonic-gate 		    &caller_ent,
19677c478bd9Sstevel@tonic-gate 		    KADM5_PRINCIPAL_NORMAL_MASK);
19687c478bd9Sstevel@tonic-gate 		if (ret.code == KADM5_OK) {
19697c478bd9Sstevel@tonic-gate 			if (caller_ent.aux_attributes & KADM5_POLICY &&
19707c478bd9Sstevel@tonic-gate 			    strcmp(caller_ent.policy, arg->name) == 0) {
19717c478bd9Sstevel@tonic-gate 		   ret.code = KADM5_OK;
197256a424ccSmp153739 	      } else ret.code = KADM5_AUTH_GET;
19737c478bd9Sstevel@tonic-gate 	      ret2 = kadm5_free_principal_ent(handle->lhandle,
19747c478bd9Sstevel@tonic-gate 					      &caller_ent);
19757c478bd9Sstevel@tonic-gate 	      ret.code = ret.code ? ret.code : ret2;
19767c478bd9Sstevel@tonic-gate 	 }
19777c478bd9Sstevel@tonic-gate     }
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate     if (ret.code == KADM5_OK) {
19807c478bd9Sstevel@tonic-gate 	 if (handle->api_version == KADM5_API_VERSION_1) {
198156a424ccSmp153739 	      ret.code  = kadm5_get_policy_v1((void *)handle, arg->name, &e);
19827c478bd9Sstevel@tonic-gate 	      if(ret.code == KADM5_OK) {
198356a424ccSmp153739 		   memcpy(&ret.rec, e, sizeof(kadm5_policy_ent_rec));
19847c478bd9Sstevel@tonic-gate 		   free(e);
19857c478bd9Sstevel@tonic-gate 	      }
19867c478bd9Sstevel@tonic-gate 	 } else {
19877c478bd9Sstevel@tonic-gate 	      ret.code = kadm5_get_policy((void *)handle, arg->name,
19887c478bd9Sstevel@tonic-gate 					  &ret.rec);
19897c478bd9Sstevel@tonic-gate 	 }
19907c478bd9Sstevel@tonic-gate 
1991159d09a2SMark Phalan 	/* Solaris Kerberos */
1992159d09a2SMark Phalan 	 if( ret.code != 0 )
1993159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
1994159d09a2SMark Phalan 
19957c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
19967c478bd9Sstevel@tonic-gate 				funcname, prime_arg, client_name, ret.code);
1997159d09a2SMark Phalan 	 log_done(funcname,
1998159d09a2SMark Phalan 		  ((prime_arg == NULL) ? "(null)" : prime_arg),
1999159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
20007c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
2001159d09a2SMark Phalan 
2002159d09a2SMark Phalan 	 if (errmsg != NULL)
2003159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
2004159d09a2SMark Phalan 
20057c478bd9Sstevel@tonic-gate     } else {
20067c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
20077c478bd9Sstevel@tonic-gate 				    funcname, prime_arg, client_name);
2008159d09a2SMark Phalan 	 log_unauth(funcname, prime_arg,
2009159d09a2SMark Phalan 		    client_name, service_name, client_addr(rqstp, buf));
20107c478bd9Sstevel@tonic-gate     }
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate error:
20137c478bd9Sstevel@tonic-gate 	if (name)
20147c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
20157c478bd9Sstevel@tonic-gate 	free_server_handle(handle);
20167c478bd9Sstevel@tonic-gate 	if (client_name)
20177c478bd9Sstevel@tonic-gate 		free(client_name);
20187c478bd9Sstevel@tonic-gate 	if (service_name)
20197c478bd9Sstevel@tonic-gate 		free(service_name);
20207c478bd9Sstevel@tonic-gate 	return (&ret);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate gpols_ret *
get_pols_2_svc(gpols_arg * arg,struct svc_req * rqstp)2025159d09a2SMark Phalan get_pols_2_svc(gpols_arg *arg, struct svc_req *rqstp)
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate     static gpols_ret		    ret;
20287c478bd9Sstevel@tonic-gate     char			    *prime_arg = NULL;
20297c478bd9Sstevel@tonic-gate     char *client_name = NULL, *service_name = NULL;
20307c478bd9Sstevel@tonic-gate     OM_uint32 min_stat;
20317c478bd9Sstevel@tonic-gate     kadm5_server_handle_t handle;
2032159d09a2SMark Phalan     const char                            *errmsg = NULL;
20337c478bd9Sstevel@tonic-gate     gss_name_t name = NULL;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate     xdr_free(xdr_gpols_ret, (char *) &ret);
20367c478bd9Sstevel@tonic-gate 
203756a424ccSmp153739     if ((ret.code = new_server_handle(arg->api_version, rqstp, &handle)))
203856a424ccSmp153739 	 return &ret;
20397c478bd9Sstevel@tonic-gate 
204056a424ccSmp153739     if ((ret.code = check_handle((void *)handle)))
20417c478bd9Sstevel@tonic-gate 		goto error;
204256a424ccSmp153739 
20437c478bd9Sstevel@tonic-gate     ret.api_version = handle->api_version;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate     if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
20467c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_FAILURE;
20477c478bd9Sstevel@tonic-gate 	goto error;
20487c478bd9Sstevel@tonic-gate     }
20497c478bd9Sstevel@tonic-gate     prime_arg = arg->exp;
20507c478bd9Sstevel@tonic-gate     if (prime_arg == NULL)
20517c478bd9Sstevel@tonic-gate 	 prime_arg = "*";
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
20547c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
20557c478bd9Sstevel@tonic-gate 		goto error;
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 
205856a424ccSmp153739     if (CHANGEPW_SERVICE(rqstp) || !kadm5int_acl_check(handle->context,
20597c478bd9Sstevel@tonic-gate 					      name,
20607c478bd9Sstevel@tonic-gate 					      ACL_LIST, NULL, NULL)) {
20617c478bd9Sstevel@tonic-gate 	 ret.code = KADM5_AUTH_LIST;
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 		audit_kadmind_unauth(rqstp->rq_xprt, l_port,
20647c478bd9Sstevel@tonic-gate 				    "kadm5_get_policies",
20657c478bd9Sstevel@tonic-gate 				    prime_arg, client_name);
206646736d35Ssemery 	 log_unauth("kadm5_get_policies", prime_arg,
206746736d35Ssemery 		    client_name, service_name, client_addr(rqstp, buf));
20687c478bd9Sstevel@tonic-gate     } else {
20697c478bd9Sstevel@tonic-gate 	 ret.code  = kadm5_get_policies((void *)handle,
20707c478bd9Sstevel@tonic-gate 					       arg->exp, &ret.pols,
20717c478bd9Sstevel@tonic-gate 					       &ret.count);
2072159d09a2SMark Phalan 	/* Solaris Kerberos */
2073159d09a2SMark Phalan 	 if( ret.code != 0 )
2074159d09a2SMark Phalan 	     errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 		audit_kadmind_auth(rqstp->rq_xprt, l_port,
20777c478bd9Sstevel@tonic-gate 				"kadm5_get_policies",
20787c478bd9Sstevel@tonic-gate 				prime_arg, client_name, ret.code);
207946736d35Ssemery 	 log_done("kadm5_get_policies", prime_arg,
2080159d09a2SMark Phalan 		  errmsg ? errmsg : "success",
20817c478bd9Sstevel@tonic-gate 		  client_name, service_name, client_addr(rqstp, buf));
2082159d09a2SMark Phalan 
2083159d09a2SMark Phalan 	  if (errmsg != NULL)
2084159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
20857c478bd9Sstevel@tonic-gate     }
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate error:
20887c478bd9Sstevel@tonic-gate 	if (name)
20897c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
20907c478bd9Sstevel@tonic-gate 	free_server_handle(handle);
20917c478bd9Sstevel@tonic-gate 	if (client_name)
20927c478bd9Sstevel@tonic-gate 		free(client_name);
20937c478bd9Sstevel@tonic-gate 	if (service_name)
20947c478bd9Sstevel@tonic-gate 		free(service_name);
20957c478bd9Sstevel@tonic-gate 	return (&ret);
20967c478bd9Sstevel@tonic-gate }
20977c478bd9Sstevel@tonic-gate 
get_privs_2_svc(krb5_ui_4 * arg,struct svc_req * rqstp)2098159d09a2SMark Phalan getprivs_ret * get_privs_2_svc(krb5_ui_4 *arg, struct svc_req *rqstp)
20997c478bd9Sstevel@tonic-gate {
21007c478bd9Sstevel@tonic-gate      static getprivs_ret	    ret;
21017c478bd9Sstevel@tonic-gate      char *client_name = NULL, *service_name = NULL;
21027c478bd9Sstevel@tonic-gate      OM_uint32 min_stat;
21037c478bd9Sstevel@tonic-gate      kadm5_server_handle_t handle;
2104159d09a2SMark Phalan      const char                           *errmsg = NULL;
21057c478bd9Sstevel@tonic-gate      gss_name_t name = NULL;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate      xdr_free(xdr_getprivs_ret, (char *) &ret);
21087c478bd9Sstevel@tonic-gate 
210956a424ccSmp153739      if ((ret.code = new_server_handle(*arg, rqstp, &handle)))
211056a424ccSmp153739 	  return &ret;
21117c478bd9Sstevel@tonic-gate 
211256a424ccSmp153739      if ((ret.code = check_handle((void *)handle)))
21137c478bd9Sstevel@tonic-gate 		goto error;
211456a424ccSmp153739 
21157c478bd9Sstevel@tonic-gate      ret.api_version = handle->api_version;
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate      if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
21187c478bd9Sstevel@tonic-gate 	  ret.code = KADM5_FAILURE;
21197c478bd9Sstevel@tonic-gate 	  goto error;
21207c478bd9Sstevel@tonic-gate      }
21217c478bd9Sstevel@tonic-gate 	if (!(name = get_clnt_name(rqstp))) {
21227c478bd9Sstevel@tonic-gate 		ret.code = KADM5_FAILURE;
21237c478bd9Sstevel@tonic-gate 		goto error;
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	ret.code = __kadm5_get_priv((void *) handle, &ret.privs, name);
2127159d09a2SMark Phalan 	/* Solaris Kerberos */
2128159d09a2SMark Phalan      if( ret.code != 0 )
2129159d09a2SMark Phalan 	 errmsg = krb5_get_error_message(handle ? handle->context : NULL, ret.code);
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	audit_kadmind_auth(rqstp->rq_xprt, l_port,
21327c478bd9Sstevel@tonic-gate 			"kadm5_get_privs", NULL, client_name,
21337c478bd9Sstevel@tonic-gate 			ret.code);
213446736d35Ssemery 	log_done("kadm5_get_privs", client_name,
2135159d09a2SMark Phalan 	    errmsg ? errmsg : "success",
21367c478bd9Sstevel@tonic-gate 	    client_name, service_name, client_addr(rqstp, buf));
21377c478bd9Sstevel@tonic-gate 
2138159d09a2SMark Phalan 	if (errmsg != NULL)
2139159d09a2SMark Phalan 		krb5_free_error_message(handle ? handle->context : NULL, errmsg);
2140159d09a2SMark Phalan 
21417c478bd9Sstevel@tonic-gate error:
21427c478bd9Sstevel@tonic-gate 	if (name)
21437c478bd9Sstevel@tonic-gate 		gss_release_name(&min_stat, &name);
21447c478bd9Sstevel@tonic-gate 	free_server_handle(handle);
21457c478bd9Sstevel@tonic-gate 	if (client_name)
21467c478bd9Sstevel@tonic-gate 		free(client_name);
21477c478bd9Sstevel@tonic-gate 	if (service_name)
21487c478bd9Sstevel@tonic-gate 		free(service_name);
21497c478bd9Sstevel@tonic-gate 	return (&ret);
21507c478bd9Sstevel@tonic-gate }
21517c478bd9Sstevel@tonic-gate 
init_2_svc(krb5_ui_4 * arg,struct svc_req * rqstp)2152159d09a2SMark Phalan generic_ret *init_2_svc(krb5_ui_4 *arg, struct svc_req *rqstp)
21537c478bd9Sstevel@tonic-gate {
21547c478bd9Sstevel@tonic-gate      static generic_ret		ret;
21557c478bd9Sstevel@tonic-gate      char *client_name, *service_name;
21567c478bd9Sstevel@tonic-gate      kadm5_server_handle_t handle;
2157159d09a2SMark Phalan      const char                       *errmsg = NULL;
215846736d35Ssemery      size_t clen, slen;
215946736d35Ssemery      char *cdots, *sdots;
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate      xdr_free(xdr_generic_ret, (char *) &ret);
21627c478bd9Sstevel@tonic-gate 
216356a424ccSmp153739      if ((ret.code = new_server_handle(*arg, rqstp, &handle)))
216456a424ccSmp153739 	  return &ret;
21657c478bd9Sstevel@tonic-gate      if (! (ret.code = check_handle((void *)handle))) {
21667c478bd9Sstevel@tonic-gate 	 ret.api_version = handle->api_version;
21677c478bd9Sstevel@tonic-gate      }
216856a424ccSmp153739 
21697c478bd9Sstevel@tonic-gate      free_server_handle(handle);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate      if (setup_gss_names(rqstp, &client_name, &service_name) < 0) {
21727c478bd9Sstevel@tonic-gate 	  ret.code = KADM5_FAILURE;
217356a424ccSmp153739 	  return &ret;
21747c478bd9Sstevel@tonic-gate      }
21757c478bd9Sstevel@tonic-gate 
2176159d09a2SMark Phalan 	/* Solaris Kerberos */
2177159d09a2SMark Phalan      if (ret.code != 0)
2178*b429bb60SZdenek Kotala 	 errmsg = krb5_get_error_message(NULL, ret.code);
2179159d09a2SMark Phalan 
21807c478bd9Sstevel@tonic-gate 	audit_kadmind_auth(rqstp->rq_xprt, l_port,
21817c478bd9Sstevel@tonic-gate 			(ret.api_version == KADM5_API_VERSION_1 ?
21827c478bd9Sstevel@tonic-gate 			"kadm5_init (V1)" : "kadm5_init"),
21837c478bd9Sstevel@tonic-gate 			NULL, client_name, ret.code);
218446736d35Ssemery 
218546736d35Ssemery      clen = strlen(client_name);
218646736d35Ssemery      trunc_name(&clen, &cdots);
218746736d35Ssemery      slen = strlen(service_name);
218846736d35Ssemery      trunc_name(&slen, &sdots);
2189159d09a2SMark Phalan      krb5_klog_syslog(LOG_NOTICE, "Request: %s, %.*s%s, %s, "
219046736d35Ssemery 		      "client=%.*s%s, service=%.*s%s, addr=%s, flavor=%d",
21917c478bd9Sstevel@tonic-gate 		      (ret.api_version == KADM5_API_VERSION_1 ?
21927c478bd9Sstevel@tonic-gate 		       "kadm5_init (V1)" : "kadm5_init"),
219346736d35Ssemery 		      clen, client_name, cdots,
2194159d09a2SMark Phalan 		      errmsg ? errmsg : "success",
219546736d35Ssemery 		      clen, client_name, cdots,
219646736d35Ssemery 		      slen, service_name, sdots,
219746736d35Ssemery 		      client_addr(rqstp, buf),
219846736d35Ssemery 		      rqstp->rq_cred.oa_flavor);
2199159d09a2SMark Phalan 	if (errmsg != NULL)
2200*b429bb60SZdenek Kotala 		krb5_free_error_message(NULL, errmsg);
22017c478bd9Sstevel@tonic-gate 	free(client_name);
22027c478bd9Sstevel@tonic-gate 	free(service_name);
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	return (&ret);
22057c478bd9Sstevel@tonic-gate }
2206