Fix some typos.
[heimdal.git] / lib / gssapi / mech / gss_pname_to_uid.c
blob315f0e0d8147c45404edf8ac6414166656972c85
1 /*
2 * Copyright (c) 2011, PADL Software Pty Ltd.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 #include "mech_locl.h"
35 static OM_uint32
36 mech_localname(OM_uint32 *minor_status,
37 struct _gss_mechanism_name *mn,
38 gss_buffer_t localname)
40 OM_uint32 major_status = GSS_S_UNAVAILABLE;
42 *minor_status = 0;
44 if (mn->gmn_mech->gm_localname == NULL)
45 return GSS_S_UNAVAILABLE;
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);
54 return major_status;
57 static OM_uint32
58 attr_localname(OM_uint32 *minor_status,
59 struct _gss_mechanism_name *mn,
60 gss_buffer_t localname)
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;
69 *minor_status = 0;
71 localname->length = 0;
72 localname->value = NULL;
74 if (mn->gmn_mech->gm_get_name_attribute == NULL)
75 return GSS_S_UNAVAILABLE;
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;
90 if (authenticated) {
91 *localname = value;
92 } else {
93 major_status = GSS_S_UNAVAILABLE;
94 gss_release_buffer(&tmpMinor, &value);
97 gss_release_buffer(&tmpMinor, &display_value);
99 return major_status;
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)
108 OM_uint32 major_status = GSS_S_UNAVAILABLE;
109 struct _gss_name *name = (struct _gss_name *) pname;
110 struct _gss_mechanism_name *mn = NULL;
112 *minor_status = 0;
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;
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;
132 if (major_status != GSS_S_COMPLETE && mn != NULL)
133 _gss_mg_error(mn->gmn_mech, major_status, *minor_status);
135 return major_status;
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)
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
158 major = gss_localname(minor_status, pname, mech_type, &localname);
159 if (GSS_ERROR(major))
160 return major;
162 szLocalname = malloc(localname.length + 1);
163 if (szLocalname == NULL) {
164 gss_release_buffer(&tmpMinor, &localname);
165 *minor_status = ENOMEM;
166 return GSS_S_FAILURE;
169 memcpy(szLocalname, localname.value, localname.length);
170 szLocalname[localname.length] = '\0';
172 #ifdef POSIX_GETPWNAM_R
173 if (getpwnam_r(szLocalname, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0)
174 pwd = NULL;
175 #else
176 pwd = getpwnam(szLocalname);
177 #endif
179 gss_release_buffer(&tmpMinor, &localname);
180 free(szLocalname);
182 *minor_status = 0;
184 if (pwd != NULL) {
185 *uidp = pwd->pw_uid;
186 major = GSS_S_COMPLETE;
187 } else {
188 major = GSS_S_UNAVAILABLE;
191 return major;
192 #endif