xref: /freebsd/crypto/heimdal/lib/gssapi/krb5/pname_to_uid.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 "gsskrb5_locl.h"
34*ae771770SStanislav Sedov 
35*ae771770SStanislav Sedov OM_uint32 GSSAPI_CALLCONV
_gsskrb5_pname_to_uid(OM_uint32 * minor_status,const gss_name_t pname,const gss_OID mech_type,uid_t * uidp)36*ae771770SStanislav Sedov _gsskrb5_pname_to_uid(OM_uint32 *minor_status,
37*ae771770SStanislav Sedov                       const gss_name_t pname,
38*ae771770SStanislav Sedov                       const gss_OID mech_type,
39*ae771770SStanislav Sedov                       uid_t *uidp)
40*ae771770SStanislav Sedov {
41*ae771770SStanislav Sedov #ifdef NO_LOCALNAME
42*ae771770SStanislav Sedov     *minor_status = KRB5_NO_LOCALNAME;
43*ae771770SStanislav Sedov     return GSS_S_FAILURE;
44*ae771770SStanislav Sedov #else
45*ae771770SStanislav Sedov     krb5_error_code ret;
46*ae771770SStanislav Sedov     krb5_context context;
47*ae771770SStanislav Sedov     krb5_const_principal princ = (krb5_const_principal)pname;
48*ae771770SStanislav Sedov     char localname[256];
49*ae771770SStanislav Sedov #ifdef POSIX_GETPWNAM_R
50*ae771770SStanislav Sedov     char pwbuf[2048];
51*ae771770SStanislav Sedov     struct passwd pw, *pwd;
52*ae771770SStanislav Sedov #else
53*ae771770SStanislav Sedov     struct passwd *pwd;
54*ae771770SStanislav Sedov #endif
55*ae771770SStanislav Sedov 
56*ae771770SStanislav Sedov     GSSAPI_KRB5_INIT(&context);
57*ae771770SStanislav Sedov 
58*ae771770SStanislav Sedov     *minor_status = 0;
59*ae771770SStanislav Sedov 
60*ae771770SStanislav Sedov     ret = krb5_aname_to_localname(context, princ,
61*ae771770SStanislav Sedov                                   sizeof(localname), localname);
62*ae771770SStanislav Sedov     if (ret != 0) {
63*ae771770SStanislav Sedov         *minor_status = ret;
64*ae771770SStanislav Sedov         return GSS_S_FAILURE;
65*ae771770SStanislav Sedov     }
66*ae771770SStanislav Sedov 
67*ae771770SStanislav Sedov #ifdef POSIX_GETPWNAM_R
68*ae771770SStanislav Sedov     if (getpwnam_r(localname, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0) {
69*ae771770SStanislav Sedov         *minor_status = KRB5_NO_LOCALNAME;
70*ae771770SStanislav Sedov         return GSS_S_FAILURE;
71*ae771770SStanislav Sedov     }
72*ae771770SStanislav Sedov #else
73*ae771770SStanislav Sedov     pwd = getpwnam(localname);
74*ae771770SStanislav Sedov #endif
75*ae771770SStanislav Sedov 
76*ae771770SStanislav Sedov     if (pwd == NULL) {
77*ae771770SStanislav Sedov         *minor_status = KRB5_NO_LOCALNAME;
78*ae771770SStanislav Sedov         return GSS_S_FAILURE;
79*ae771770SStanislav Sedov     }
80*ae771770SStanislav Sedov 
81*ae771770SStanislav Sedov     *uidp = pwd->pw_uid;
82*ae771770SStanislav Sedov 
83*ae771770SStanislav Sedov     return GSS_S_COMPLETE;
84*ae771770SStanislav Sedov #endif /* NO_LOCALNAME */
85*ae771770SStanislav Sedov }
86