Debian Codegraph @

lib/gssapi/mech/gss_pname_to_uid.c

raw
1 /*
2 * Copyright (c) 2011, PADL Software Pty Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of PADL Software nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "mech_locl.h"
34
35 static OM_uint32
36 mech_localname(OM_uint32 *minor_status,
37 struct _gss_mechanism_name *mn,
38 gss_buffer_t localname)
39 {
40 OM_uint32 major_status = GSS_S_UNAVAILABLE;
41
42 *minor_status = 0;
43
44 if (mn->gmn_mech->gm_localname == NULL)
45 return GSS_S_UNAVAILABLE;
46
47 major_status = mn->gmn_mech->gm_localname(minor_status,
48 mn->gmn_name,
49 mn->gmn_mech_oid,
50 localname);
51 if (GSS_ERROR(major_status))
52 _gss_mg_error(mn->gmn_mech, major_status, *minor_status);
53
54 return major_status;
55 }
56
57 static OM_uint32
58 attr_localname(OM_uint32 *minor_status,
59 struct _gss_mechanism_name *mn,
60 gss_buffer_t localname)
61 {
62 OM_uint32 major_status = GSS_S_UNAVAILABLE;
63 OM_uint32 tmpMinor;
64 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
65 gss_buffer_desc display_value = GSS_C_EMPTY_BUFFER;
66 int authenticated = 0, complete = 0;
67 int more = -1;
68
69 *minor_status = 0;
70
71 localname->length = 0;
72 localname->value = NULL;
73
74 if (mn->gmn_mech->gm_get_name_attribute == NULL)
75 return GSS_S_UNAVAILABLE;
76
77 major_status = mn->gmn_mech->gm_get_name_attribute(minor_status,
78 mn->gmn_name,
79 GSS_C_ATTR_LOCAL_LOGIN_USER,
80 &authenticated,
81 &complete,
82 &value,
83 &display_value,
84 &more);
85 if (GSS_ERROR(major_status)) {
86 _gss_mg_error(mn->gmn_mech, major_status, *minor_status);
87 return major_status;
88 }
89
90 if (authenticated) {
91 *localname = value;
92 } else {
93 major_status = GSS_S_UNAVAILABLE;
94 gss_release_buffer(&tmpMinor, &value);
95 }
96
97 gss_release_buffer(&tmpMinor, &display_value);
98
99 return major_status;
100 }
101
102 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
103 gss_localname(OM_uint32 *minor_status,
104 gss_const_name_t pname,
105 const gss_OID mech_type,
106 gss_buffer_t localname)
107 {
108 OM_uint32 major_status = GSS_S_UNAVAILABLE;
109 struct _gss_name *name = (struct _gss_name *) pname;
110 struct _gss_mechanism_name *mn = NULL;
111
112 *minor_status = 0;
113
114 if (mech_type != GSS_C_NO_OID) {
115 major_status = _gss_find_mn(minor_status, name, mech_type, &mn);
116 if (GSS_ERROR(major_status))
117 return major_status;
118
119 major_status = mech_localname(minor_status, mn, localname);
120 if (major_status != GSS_S_COMPLETE)
121 major_status = attr_localname(minor_status, mn, localname);
122 } else {
123 HEIM_SLIST_FOREACH(mn, &name->gn_mn, gmn_link) {
124 major_status = mech_localname(minor_status, mn, localname);
125 if (major_status != GSS_S_COMPLETE)
126 major_status = attr_localname(minor_status, mn, localname);
127 if (major_status != GSS_S_UNAVAILABLE)
128 break;
129 }
130 }
131
132 if (major_status != GSS_S_COMPLETE && mn != NULL)
133 _gss_mg_error(mn->gmn_mech, major_status, *minor_status);
134
135 return major_status;
136 }
137
138
139 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
140 gss_pname_to_uid(OM_uint32 *minor_status,
141 gss_const_name_t pname,
142 const gss_OID mech_type,
143 uid_t *uidp)
144 {
145 #ifdef NO_LOCALNAME
146 return GSS_S_UNAVAILABLE;
147 #else
148 OM_uint32 major, tmpMinor;
149 gss_buffer_desc localname = GSS_C_EMPTY_BUFFER;
150 char *szLocalname;
151 #ifdef POSIX_GETPWNAM_R
152 char pwbuf[2048];
153 struct passwd pw, *pwd;
154 #else
155 struct passwd *pwd;
156 #endif
157
158 major = gss_localname(minor_status, pname, mech_type, &localname);
159 if (GSS_ERROR(major))
160 return major;
161 if (localname.length == 0) {
162 *minor_status = KRB5_NO_LOCALNAME;
163 return GSS_S_FAILURE;
164 }
165
166 szLocalname = malloc(localname.length + 1);
167 if (szLocalname == NULL) {
168 gss_release_buffer(&tmpMinor, &localname);
169 *minor_status = ENOMEM;
170 return GSS_S_FAILURE;
171 }
172
173 memcpy(szLocalname, localname.value, localname.length);
174 szLocalname[localname.length] = '\0';
175
176 #ifdef POSIX_GETPWNAM_R
177 if (getpwnam_r(szLocalname, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0)
178 pwd = NULL;
179 #else
180 pwd = getpwnam(szLocalname);
181 #endif
182
183 gss_release_buffer(&tmpMinor, &localname);
184 free(szLocalname);
185
186 *minor_status = 0;
187
188 if (pwd != NULL) {
189 *uidp = pwd->pw_uid;
190 major = GSS_S_COMPLETE;
191 } else {
192 major = GSS_S_UNAVAILABLE;
193 }
194
195 return major;
196 #endif
197 }