xref: /illumos-gate/usr/src/lib/libgss/g_userok.c (revision 1da57d55)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <unistd.h>
30*7c478bd9Sstevel@tonic-gate #include <deflt.h>
31*7c478bd9Sstevel@tonic-gate #include <mechglueP.h>
32*7c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
33*7c478bd9Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate static OM_uint32
compare_names(OM_uint32 * minor,const gss_OID mech_type,const gss_name_t name,const char * user,int * user_ok)37*7c478bd9Sstevel@tonic-gate compare_names(OM_uint32 *minor,
38*7c478bd9Sstevel@tonic-gate 	    const gss_OID mech_type,
39*7c478bd9Sstevel@tonic-gate 	    const gss_name_t name,
40*7c478bd9Sstevel@tonic-gate 	    const char *user,
41*7c478bd9Sstevel@tonic-gate 	    int *user_ok)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	OM_uint32 status, tmpMinor;
45*7c478bd9Sstevel@tonic-gate 	gss_name_t imported_name;
46*7c478bd9Sstevel@tonic-gate 	gss_name_t canon_name;
47*7c478bd9Sstevel@tonic-gate 	gss_buffer_desc gss_user;
48*7c478bd9Sstevel@tonic-gate 	int match = 0;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	*user_ok = 0;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	gss_user.value = (void *)user;
53*7c478bd9Sstevel@tonic-gate 	if (!gss_user.value || !name || !mech_type)
54*7c478bd9Sstevel@tonic-gate 		return (GSS_S_BAD_NAME);
55*7c478bd9Sstevel@tonic-gate 	gss_user.length = strlen(gss_user.value);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	status = gss_import_name(minor,
58*7c478bd9Sstevel@tonic-gate 				&gss_user,
59*7c478bd9Sstevel@tonic-gate 				GSS_C_NT_USER_NAME,
60*7c478bd9Sstevel@tonic-gate 				&imported_name);
61*7c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
62*7c478bd9Sstevel@tonic-gate 		goto out;
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	status = gss_canonicalize_name(minor,
66*7c478bd9Sstevel@tonic-gate 				    imported_name,
67*7c478bd9Sstevel@tonic-gate 				    mech_type,
68*7c478bd9Sstevel@tonic-gate 				    &canon_name);
69*7c478bd9Sstevel@tonic-gate 	if (status != GSS_S_COMPLETE) {
70*7c478bd9Sstevel@tonic-gate 		(void) gss_release_name(&tmpMinor, &imported_name);
71*7c478bd9Sstevel@tonic-gate 		goto out;
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	status = gss_compare_name(minor,
75*7c478bd9Sstevel@tonic-gate 				canon_name,
76*7c478bd9Sstevel@tonic-gate 				name,
77*7c478bd9Sstevel@tonic-gate 				&match);
78*7c478bd9Sstevel@tonic-gate 	(void) gss_release_name(&tmpMinor, &canon_name);
79*7c478bd9Sstevel@tonic-gate 	(void) gss_release_name(&tmpMinor, &imported_name);
80*7c478bd9Sstevel@tonic-gate 	if (status == GSS_S_COMPLETE) {
81*7c478bd9Sstevel@tonic-gate 		if (match)
82*7c478bd9Sstevel@tonic-gate 			*user_ok = 1; /* remote user is a-ok */
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate out:
86*7c478bd9Sstevel@tonic-gate 	return (status);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate OM_uint32
__gss_userok(OM_uint32 * minor,const gss_name_t name,const char * user,int * user_ok)91*7c478bd9Sstevel@tonic-gate __gss_userok(OM_uint32 *minor,
92*7c478bd9Sstevel@tonic-gate 	    const gss_name_t name,
93*7c478bd9Sstevel@tonic-gate 	    const char *user,
94*7c478bd9Sstevel@tonic-gate 	    int *user_ok)
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	gss_mechanism mech;
98*7c478bd9Sstevel@tonic-gate 	gss_union_name_t intName;
99*7c478bd9Sstevel@tonic-gate 	gss_name_t mechName = NULL;
100*7c478bd9Sstevel@tonic-gate 	OM_uint32 major;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if (minor == NULL || user_ok == NULL)
103*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	if (name == NULL || user == NULL)
106*7c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	*user_ok = 0;
109*7c478bd9Sstevel@tonic-gate 	*minor = GSS_S_COMPLETE;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	intName = (gss_union_name_t)name;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	mech = __gss_get_mechanism(intName->mech_type);
114*7c478bd9Sstevel@tonic-gate 	if (mech == NULL)
115*7c478bd9Sstevel@tonic-gate 		return (GSS_S_UNAVAILABLE);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	/* may need to import the name if this is not MN */
118*7c478bd9Sstevel@tonic-gate 	if (intName->mech_type == NULL) {
119*7c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
120*7c478bd9Sstevel@tonic-gate 	} else
121*7c478bd9Sstevel@tonic-gate 		mechName = intName->mech_name;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (mech->__gss_userok)
124*7c478bd9Sstevel@tonic-gate 		major = mech->__gss_userok(mech->context,  minor, mechName,
125*7c478bd9Sstevel@tonic-gate 				user, user_ok);
126*7c478bd9Sstevel@tonic-gate 	else
127*7c478bd9Sstevel@tonic-gate 		major = compare_names(minor, intName->mech_type,
128*7c478bd9Sstevel@tonic-gate 				    name, user, user_ok);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	return (major);
131*7c478bd9Sstevel@tonic-gate } /* gss_userok */
132