xref: /freebsd/crypto/heimdal/lib/gssapi/mech/gss_authorize_localname.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1*ae771770SStanislav Sedov /*
2*ae771770SStanislav Sedov  * Copyright (c) 2011, PADL Software Pty Ltd.
3*ae771770SStanislav Sedov  * All rights reserved.
4*ae771770SStanislav Sedov  *
5*ae771770SStanislav Sedov  * Redistribution and use in source and binary forms, with or without
6*ae771770SStanislav Sedov  * modification, are permitted provided that the following conditions
7*ae771770SStanislav Sedov  * are met:
8*ae771770SStanislav Sedov  *
9*ae771770SStanislav Sedov  * 1. Redistributions of source code must retain the above copyright
10*ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer.
11*ae771770SStanislav Sedov  *
12*ae771770SStanislav Sedov  * 2. Redistributions in binary form must reproduce the above copyright
13*ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer in the
14*ae771770SStanislav Sedov  *    documentation and/or other materials provided with the distribution.
15*ae771770SStanislav Sedov  *
16*ae771770SStanislav Sedov  * 3. Neither the name of PADL Software nor the names of its contributors
17*ae771770SStanislav Sedov  *    may be used to endorse or promote products derived from this software
18*ae771770SStanislav Sedov  *    without specific prior written permission.
19*ae771770SStanislav Sedov  *
20*ae771770SStanislav Sedov  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21*ae771770SStanislav Sedov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*ae771770SStanislav Sedov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*ae771770SStanislav Sedov  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24*ae771770SStanislav Sedov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*ae771770SStanislav Sedov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*ae771770SStanislav Sedov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*ae771770SStanislav Sedov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*ae771770SStanislav Sedov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*ae771770SStanislav Sedov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*ae771770SStanislav Sedov  * SUCH DAMAGE.
31*ae771770SStanislav Sedov  */
32*ae771770SStanislav Sedov 
33*ae771770SStanislav Sedov #include "mech_locl.h"
34*ae771770SStanislav Sedov 
35*ae771770SStanislav Sedov gss_buffer_desc GSSAPI_LIB_VARIABLE __gss_c_attr_local_login_user =  {
36*ae771770SStanislav Sedov     sizeof("local-login-user") - 1,
37*ae771770SStanislav Sedov     "local-login-user"
38*ae771770SStanislav Sedov };
39*ae771770SStanislav Sedov 
40*ae771770SStanislav Sedov static OM_uint32
mech_authorize_localname(OM_uint32 * minor_status,const struct _gss_name * name,const struct _gss_name * user)41*ae771770SStanislav Sedov mech_authorize_localname(OM_uint32 *minor_status,
42*ae771770SStanislav Sedov 	                 const struct _gss_name *name,
43*ae771770SStanislav Sedov 	                 const struct _gss_name *user)
44*ae771770SStanislav Sedov {
45*ae771770SStanislav Sedov     OM_uint32 major_status = GSS_S_NAME_NOT_MN;
46*ae771770SStanislav Sedov     struct _gss_mechanism_name *mn;
47*ae771770SStanislav Sedov 
48*ae771770SStanislav Sedov     HEIM_SLIST_FOREACH(mn, &name->gn_mn, gmn_link) {
49*ae771770SStanislav Sedov         gssapi_mech_interface m = mn->gmn_mech;
50*ae771770SStanislav Sedov 
51*ae771770SStanislav Sedov         if (m->gm_authorize_localname == NULL) {
52*ae771770SStanislav Sedov             major_status = GSS_S_UNAVAILABLE;
53*ae771770SStanislav Sedov             continue;
54*ae771770SStanislav Sedov         }
55*ae771770SStanislav Sedov 
56*ae771770SStanislav Sedov         major_status = m->gm_authorize_localname(minor_status,
57*ae771770SStanislav Sedov                                                  mn->gmn_name,
58*ae771770SStanislav Sedov                                                  &user->gn_value,
59*ae771770SStanislav Sedov                                                  &user->gn_type);
60*ae771770SStanislav Sedov         if (major_status != GSS_S_UNAUTHORIZED)
61*ae771770SStanislav Sedov             break;
62*ae771770SStanislav Sedov     }
63*ae771770SStanislav Sedov 
64*ae771770SStanislav Sedov     return major_status;
65*ae771770SStanislav Sedov }
66*ae771770SStanislav Sedov 
67*ae771770SStanislav Sedov /*
68*ae771770SStanislav Sedov  * Naming extensions based local login authorization.
69*ae771770SStanislav Sedov  */
70*ae771770SStanislav Sedov static OM_uint32
attr_authorize_localname(OM_uint32 * minor_status,const struct _gss_name * name,const struct _gss_name * user)71*ae771770SStanislav Sedov attr_authorize_localname(OM_uint32 *minor_status,
72*ae771770SStanislav Sedov 	                 const struct _gss_name *name,
73*ae771770SStanislav Sedov 	                 const struct _gss_name *user)
74*ae771770SStanislav Sedov {
75*ae771770SStanislav Sedov     OM_uint32 major_status = GSS_S_UNAVAILABLE;
76*ae771770SStanislav Sedov     int more = -1;
77*ae771770SStanislav Sedov 
78*ae771770SStanislav Sedov     if (!gss_oid_equal(&user->gn_type, GSS_C_NT_USER_NAME))
79*ae771770SStanislav Sedov         return GSS_S_BAD_NAMETYPE;
80*ae771770SStanislav Sedov 
81*ae771770SStanislav Sedov     while (more != 0 && major_status != GSS_S_COMPLETE) {
82*ae771770SStanislav Sedov 	OM_uint32 tmpMajor, tmpMinor;
83*ae771770SStanislav Sedov 	gss_buffer_desc value;
84*ae771770SStanislav Sedov 	gss_buffer_desc display_value;
85*ae771770SStanislav Sedov 	int authenticated = 0, complete = 0;
86*ae771770SStanislav Sedov 
87*ae771770SStanislav Sedov 	tmpMajor = gss_get_name_attribute(minor_status,
88*ae771770SStanislav Sedov 					  (gss_name_t)name,
89*ae771770SStanislav Sedov 					  GSS_C_ATTR_LOCAL_LOGIN_USER,
90*ae771770SStanislav Sedov 					  &authenticated,
91*ae771770SStanislav Sedov 					  &complete,
92*ae771770SStanislav Sedov 					  &value,
93*ae771770SStanislav Sedov 					  &display_value,
94*ae771770SStanislav Sedov 					  &more);
95*ae771770SStanislav Sedov 	if (GSS_ERROR(tmpMajor)) {
96*ae771770SStanislav Sedov 	    major_status = tmpMajor;
97*ae771770SStanislav Sedov 	    break;
98*ae771770SStanislav Sedov 	}
99*ae771770SStanislav Sedov 
100*ae771770SStanislav Sedov 	/* If attribute is present, return an authoritative error code. */
101*ae771770SStanislav Sedov 	if (authenticated &&
102*ae771770SStanislav Sedov 	    value.length == user->gn_value.length &&
103*ae771770SStanislav Sedov 	    memcmp(value.value, user->gn_value.value, user->gn_value.length) == 0)
104*ae771770SStanislav Sedov 	    major_status = GSS_S_COMPLETE;
105*ae771770SStanislav Sedov 	else
106*ae771770SStanislav Sedov 	    major_status = GSS_S_UNAUTHORIZED;
107*ae771770SStanislav Sedov 
108*ae771770SStanislav Sedov 	gss_release_buffer(&tmpMinor, &value);
109*ae771770SStanislav Sedov 	gss_release_buffer(&tmpMinor, &display_value);
110*ae771770SStanislav Sedov     }
111*ae771770SStanislav Sedov 
112*ae771770SStanislav Sedov     return major_status;
113*ae771770SStanislav Sedov }
114*ae771770SStanislav Sedov 
115*ae771770SStanislav Sedov GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
gss_authorize_localname(OM_uint32 * minor_status,const gss_name_t gss_name,const gss_name_t gss_user)116*ae771770SStanislav Sedov gss_authorize_localname(OM_uint32 *minor_status,
117*ae771770SStanislav Sedov 	                const gss_name_t gss_name,
118*ae771770SStanislav Sedov 	                const gss_name_t gss_user)
119*ae771770SStanislav Sedov 
120*ae771770SStanislav Sedov {
121*ae771770SStanislav Sedov     OM_uint32 major_status;
122*ae771770SStanislav Sedov     const struct _gss_name *name = (const struct _gss_name *) gss_name;
123*ae771770SStanislav Sedov     const struct _gss_name *user = (const struct _gss_name *) gss_user;
124*ae771770SStanislav Sedov     int mechAvailable = 0;
125*ae771770SStanislav Sedov 
126*ae771770SStanislav Sedov     *minor_status = 0;
127*ae771770SStanislav Sedov 
128*ae771770SStanislav Sedov     if (gss_name == GSS_C_NO_NAME || gss_user == GSS_C_NO_NAME)
129*ae771770SStanislav Sedov         return GSS_S_CALL_INACCESSIBLE_READ;
130*ae771770SStanislav Sedov 
131*ae771770SStanislav Sedov     /*
132*ae771770SStanislav Sedov      * We should check that the user name is not a mechanism name, but
133*ae771770SStanislav Sedov      * as Heimdal always calls the mechanism's gss_import_name(), it's
134*ae771770SStanislav Sedov      * not possible to make this check.
135*ae771770SStanislav Sedov      */
136*ae771770SStanislav Sedov #if 0
137*ae771770SStanislav Sedov     if (HEIM_SLIST_FIRST(&user->gn_mn) != NULL)
138*ae771770SStanislav Sedov         return GSS_S_BAD_NAME;
139*ae771770SStanislav Sedov #endif
140*ae771770SStanislav Sedov 
141*ae771770SStanislav Sedov     /* If mech returns yes, we return yes */
142*ae771770SStanislav Sedov     major_status = mech_authorize_localname(minor_status, name, user);
143*ae771770SStanislav Sedov     if (major_status == GSS_S_COMPLETE)
144*ae771770SStanislav Sedov 	return GSS_S_COMPLETE;
145*ae771770SStanislav Sedov     else if (major_status != GSS_S_UNAVAILABLE)
146*ae771770SStanislav Sedov 	mechAvailable = 1;
147*ae771770SStanislav Sedov 
148*ae771770SStanislav Sedov     /* If attribute exists, it is authoritative */
149*ae771770SStanislav Sedov     major_status = attr_authorize_localname(minor_status, name, user);
150*ae771770SStanislav Sedov     if (major_status == GSS_S_COMPLETE || major_status == GSS_S_UNAUTHORIZED)
151*ae771770SStanislav Sedov 	return major_status;
152*ae771770SStanislav Sedov 
153*ae771770SStanislav Sedov     /* If mechanism did not implement SPI, compare the local name */
154*ae771770SStanislav Sedov     if (mechAvailable == 0) {
155*ae771770SStanislav Sedov 	int match = 0;
156*ae771770SStanislav Sedov 
157*ae771770SStanislav Sedov         major_status = gss_compare_name(minor_status, gss_name,
158*ae771770SStanislav Sedov                                         gss_user, &match);
159*ae771770SStanislav Sedov 	if (major_status == GSS_S_COMPLETE && match == 0)
160*ae771770SStanislav Sedov 	    major_status = GSS_S_UNAUTHORIZED;
161*ae771770SStanislav Sedov     }
162*ae771770SStanislav Sedov 
163*ae771770SStanislav Sedov     return major_status;
164*ae771770SStanislav Sedov }
165*ae771770SStanislav Sedov 
166*ae771770SStanislav Sedov GSSAPI_LIB_FUNCTION int GSSAPI_LIB_CALL
gss_userok(const gss_name_t name,const char * user)167*ae771770SStanislav Sedov gss_userok(const gss_name_t name,
168*ae771770SStanislav Sedov            const char *user)
169*ae771770SStanislav Sedov {
170*ae771770SStanislav Sedov     OM_uint32 major_status, minor_status;
171*ae771770SStanislav Sedov     gss_buffer_desc userBuf;
172*ae771770SStanislav Sedov     gss_name_t userName;
173*ae771770SStanislav Sedov 
174*ae771770SStanislav Sedov     userBuf.value = (void *)user;
175*ae771770SStanislav Sedov     userBuf.length = strlen(user);
176*ae771770SStanislav Sedov 
177*ae771770SStanislav Sedov     major_status = gss_import_name(&minor_status, &userBuf,
178*ae771770SStanislav Sedov                                    GSS_C_NT_USER_NAME, &userName);
179*ae771770SStanislav Sedov     if (GSS_ERROR(major_status))
180*ae771770SStanislav Sedov         return 0;
181*ae771770SStanislav Sedov 
182*ae771770SStanislav Sedov     major_status = gss_authorize_localname(&minor_status, name, userName);
183*ae771770SStanislav Sedov 
184*ae771770SStanislav Sedov     gss_release_name(&minor_status, &userName);
185*ae771770SStanislav Sedov 
186*ae771770SStanislav Sedov     return (major_status == GSS_S_COMPLETE);
187*ae771770SStanislav Sedov }
188