summaryrefslogtreecommitdiffstats
path: root/src/lib/gssapi/krb5/import_name.c
blob: 6879c766faa000e13ea4739eff7ae3531ec014f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* -*- mode: c; indent-tabs-mode: nil -*- */
/*
 * Copyright 1993 by OpenVision Technologies, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of OpenVision not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission. OpenVision makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * $Id$
 */

#include "gssapiP_krb5.h"

#ifndef NO_PASSWORD
#include <pwd.h>
#include <stdio.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

/*
 * errors:
 * GSS_S_BAD_NAMETYPE   if the type is bogus
 * GSS_S_BAD_NAME       if the type is good but the name is bogus
 * GSS_S_FAILURE        if memory allocation fails
 */

OM_uint32
krb5_gss_import_name(minor_status, input_name_buffer,
                     input_name_type, output_name)
    OM_uint32 *minor_status;
    gss_buffer_t input_name_buffer;
    gss_OID input_name_type;
    gss_name_t *output_name;
{
    krb5_context context;
    krb5_principal princ;
    krb5_error_code code;
    char *stringrep, *tmp, *tmp2, *cp;
    OM_uint32    length;
#ifndef NO_PASSWORD
    struct passwd *pw;
#endif

    code = krb5_gss_init_context(&context);
    if (code) {
        *minor_status = code;
        return GSS_S_FAILURE;
    }

    /* set up default returns */

    *output_name = NULL;
    *minor_status = 0;

    /* Go find the appropriate string rep to pass into parse_name */

    if ((input_name_type != GSS_C_NULL_OID) &&
        (g_OID_equal(input_name_type, gss_nt_service_name) ||
         g_OID_equal(input_name_type, gss_nt_service_name_v2))) {
        char *service, *host;

        if ((tmp =
             (char *) xmalloc(input_name_buffer->length + 1)) == NULL) {
            *minor_status = ENOMEM;
            krb5_free_context(context);
            return(GSS_S_FAILURE);
        }

        memcpy(tmp, input_name_buffer->value, input_name_buffer->length);
        tmp[input_name_buffer->length] = 0;

        service = tmp;
        if ((host = strchr(tmp, '@'))) {
            *host = '\0';
            host++;
        }

        code = krb5_sname_to_principal(context, host, service, KRB5_NT_SRV_HST,
                                       &princ);

        xfree(tmp);
    } else if ((input_name_type != GSS_C_NULL_OID) &&
               (g_OID_equal(input_name_type, gss_nt_krb5_principal))) {
        krb5_principal input;

        if (input_name_buffer->length != sizeof(krb5_principal)) {
            *minor_status = (OM_uint32) G_WRONG_SIZE;
            krb5_free_context(context);
            return(GSS_S_BAD_NAME);
        }

        input = *((krb5_principal *) input_name_buffer->value);

        if ((code = krb5_copy_principal(context, input, &princ))) {
            *minor_status = code;
            save_error_info(*minor_status, context);
            krb5_free_context(context);
            return(GSS_S_FAILURE);
        }
    } else {
#ifndef NO_PASSWORD
        uid_t uid;
        struct passwd pwx;
        char pwbuf[BUFSIZ];
#endif

        stringrep = NULL;

        if ((tmp =
             (char *) xmalloc(input_name_buffer->length + 1)) == NULL) {
            *minor_status = ENOMEM;
            krb5_free_context(context);
            return(GSS_S_FAILURE);
        }
        tmp2 = 0;

        memcpy(tmp, input_name_buffer->value, input_name_buffer->length);
        tmp[input_name_buffer->length] = 0;

        if ((input_name_type == GSS_C_NULL_OID) ||
            g_OID_equal(input_name_type, gss_nt_krb5_name) ||
            g_OID_equal(input_name_type, gss_nt_user_name)) {
            stringrep = (char *) tmp;
#ifndef NO_PASSWORD
        } else if (g_OID_equal(input_name_type, gss_nt_machine_uid_name)) {
            uid = *(uid_t *) input_name_buffer->value;
        do_getpwuid:
            if (k5_getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw) == 0)
                stringrep = pw->pw_name;
            else
                *minor_status = (OM_uint32) G_NOUSER;
        } else if (g_OID_equal(input_name_type, gss_nt_string_uid_name)) {
            uid = atoi(tmp);
            goto do_getpwuid;
#endif
        } else if (g_OID_equal(input_name_type, gss_nt_exported_name)) {
            cp = tmp;
            if (*cp++ != 0x04)
                goto fail_name;
            if (*cp++ != 0x01)
                goto fail_name;
            if (*cp++ != 0x00)
                goto fail_name;
            length = *cp++;
            if (length != gss_mech_krb5->length+2)
                goto fail_name;
            if (*cp++ != 0x06)
                goto fail_name;
            length = *cp++;
            if (length != gss_mech_krb5->length)
                goto fail_name;
            if (memcmp(cp, gss_mech_krb5->elements, length) != 0)
                goto fail_name;
            cp += length;
            length = *cp++;
            length = (length << 8) | *cp++;
            length = (length << 8) | *cp++;
            length = (length << 8) | *cp++;
            tmp2 = malloc(length+1);
            if (tmp2 == NULL) {
                xfree(tmp);
                *minor_status = ENOMEM;
                krb5_free_context(context);
                return GSS_S_FAILURE;
            }
            strncpy(tmp2, cp, length);
            tmp2[length] = 0;

            stringrep = tmp2;
        } else {
            xfree(tmp);
            krb5_free_context(context);
            return(GSS_S_BAD_NAMETYPE);
        }

        /* at this point, stringrep is set, or if not, *minor_status is. */

        if (stringrep)
            code = krb5_parse_name(context, (char *) stringrep, &princ);
        else {
        fail_name:
            xfree(tmp);
            if (tmp2)
                xfree(tmp2);
            krb5_free_context(context);
            return(GSS_S_BAD_NAME);
        }

        if (tmp2)
            xfree(tmp2);
        xfree(tmp);
    }

    /* at this point, a krb5 function has been called to set princ.  code
       contains the return status */

    if (code) {
        *minor_status = (OM_uint32) code;
        save_error_info(*minor_status, context);
        krb5_free_context(context);
        return(GSS_S_BAD_NAME);
    }

    /* save the name in the validation database */

    if (! kg_save_name((gss_name_t) princ)) {
        krb5_free_principal(context, princ);
        krb5_free_context(context);
        *minor_status = (OM_uint32) G_VALIDATE_FAILED;
        return(GSS_S_FAILURE);
    }

    krb5_free_context(context);

    /* return it */

    *output_name = (gss_name_t) princ;
    return(GSS_S_COMPLETE);
}