1226031Sstas/*
2226031Sstas * Copyright (c) 2011, PADL Software Pty Ltd.
3226031Sstas * All rights reserved.
4226031Sstas *
5226031Sstas * Redistribution and use in source and binary forms, with or without
6226031Sstas * modification, are permitted provided that the following conditions
7226031Sstas * are met:
8226031Sstas *
9226031Sstas * 1. Redistributions of source code must retain the above copyright
10226031Sstas *    notice, this list of conditions and the following disclaimer.
11226031Sstas *
12226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
13226031Sstas *    notice, this list of conditions and the following disclaimer in the
14226031Sstas *    documentation and/or other materials provided with the distribution.
15226031Sstas *
16226031Sstas * 3. Neither the name of PADL Software nor the names of its contributors
17226031Sstas *    may be used to endorse or promote products derived from this software
18226031Sstas *    without specific prior written permission.
19226031Sstas *
20226031Sstas * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30226031Sstas * SUCH DAMAGE.
31226031Sstas */
32226031Sstas
33226031Sstas#include "gsskrb5_locl.h"
34226031Sstas
35226031SstasOM_uint32 GSSAPI_CALLCONV
36226031Sstas_gsskrb5_pname_to_uid(OM_uint32 *minor_status,
37226031Sstas                      const gss_name_t pname,
38226031Sstas                      const gss_OID mech_type,
39226031Sstas                      uid_t *uidp)
40226031Sstas{
41226031Sstas#ifdef NO_LOCALNAME
42226031Sstas    *minor_status = KRB5_NO_LOCALNAME;
43226031Sstas    return GSS_S_FAILURE;
44226031Sstas#else
45226031Sstas    krb5_error_code ret;
46226031Sstas    krb5_context context;
47226031Sstas    krb5_const_principal princ = (krb5_const_principal)pname;
48226031Sstas    char localname[256];
49226031Sstas#ifdef POSIX_GETPWNAM_R
50226031Sstas    char pwbuf[2048];
51226031Sstas    struct passwd pw, *pwd;
52226031Sstas#else
53226031Sstas    struct passwd *pwd;
54226031Sstas#endif
55226031Sstas
56226031Sstas    GSSAPI_KRB5_INIT(&context);
57226031Sstas
58226031Sstas    *minor_status = 0;
59226031Sstas
60226031Sstas    ret = krb5_aname_to_localname(context, princ,
61226031Sstas                                  sizeof(localname), localname);
62226031Sstas    if (ret != 0) {
63226031Sstas        *minor_status = ret;
64226031Sstas        return GSS_S_FAILURE;
65226031Sstas    }
66226031Sstas
67226031Sstas#ifdef POSIX_GETPWNAM_R
68226031Sstas    if (getpwnam_r(localname, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0) {
69226031Sstas        *minor_status = KRB5_NO_LOCALNAME;
70226031Sstas        return GSS_S_FAILURE;
71226031Sstas    }
72226031Sstas#else
73226031Sstas    pwd = getpwnam(localname);
74226031Sstas#endif
75226031Sstas
76226031Sstas    if (pwd == NULL) {
77226031Sstas        *minor_status = KRB5_NO_LOCALNAME;
78226031Sstas        return GSS_S_FAILURE;
79226031Sstas    }
80226031Sstas
81226031Sstas    *uidp = pwd->pw_uid;
82226031Sstas
83226031Sstas    return GSS_S_COMPLETE;
84226031Sstas#endif /* NO_LOCALNAME */
85226031Sstas}
86