Home | History | Annotate | Line # | Download | only in ntlm
      1  1.2  christos /*	$NetBSD: creds.c,v 1.5 2023/06/19 21:41:43 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 2006 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
      9  1.1     elric  *
     10  1.1     elric  * Redistribution and use in source and binary forms, with or without
     11  1.1     elric  * modification, are permitted provided that the following conditions
     12  1.1     elric  * are met:
     13  1.1     elric  *
     14  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     15  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     16  1.1     elric  *
     17  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     19  1.1     elric  *    documentation and/or other materials provided with the distribution.
     20  1.1     elric  *
     21  1.1     elric  * 3. Neither the name of the Institute nor the names of its contributors
     22  1.1     elric  *    may be used to endorse or promote products derived from this software
     23  1.1     elric  *    without specific prior written permission.
     24  1.1     elric  *
     25  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     26  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     29  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1     elric  * SUCH DAMAGE.
     36  1.1     elric  */
     37  1.1     elric 
     38  1.1     elric #include "ntlm.h"
     39  1.1     elric 
     40  1.2  christos OM_uint32 GSSAPI_CALLCONV
     41  1.2  christos _gss_ntlm_inquire_cred
     42  1.1     elric            (OM_uint32 * minor_status,
     43  1.3  christos             gss_const_cred_id_t cred_handle,
     44  1.1     elric             gss_name_t * name,
     45  1.1     elric             OM_uint32 * lifetime,
     46  1.1     elric             gss_cred_usage_t * cred_usage,
     47  1.1     elric             gss_OID_set * mechanisms
     48  1.1     elric            )
     49  1.1     elric {
     50  1.1     elric     OM_uint32 ret, junk;
     51  1.1     elric 
     52  1.1     elric     *minor_status = 0;
     53  1.1     elric 
     54  1.1     elric     if (cred_handle == NULL)
     55  1.1     elric 	return GSS_S_NO_CRED;
     56  1.1     elric 
     57  1.1     elric     if (name) {
     58  1.2  christos 	ntlm_name n = calloc(1, sizeof(*n));
     59  1.2  christos 	ntlm_cred c = (ntlm_cred)cred_handle;
     60  1.2  christos 	if (n) {
     61  1.2  christos 	    n->user = strdup(c->username);
     62  1.2  christos 	    n->domain = strdup(c->domain);
     63  1.2  christos 	}
     64  1.2  christos 	if (n == NULL || n->user == NULL || n->domain == NULL) {
     65  1.2  christos 	    if (n) {
     66  1.2  christos 		free(n->user);
     67  1.2  christos 		free(n->domain);
     68  1.2  christos 		free(n);
     69  1.2  christos 	    }
     70  1.2  christos 	    *minor_status = ENOMEM;
     71  1.2  christos 	    return GSS_S_FAILURE;
     72  1.2  christos 	}
     73  1.2  christos 	*name = (gss_name_t)n;
     74  1.1     elric     }
     75  1.1     elric     if (lifetime)
     76  1.1     elric 	*lifetime = GSS_C_INDEFINITE;
     77  1.1     elric     if (cred_usage)
     78  1.1     elric 	*cred_usage = 0;
     79  1.1     elric     if (mechanisms)
     80  1.1     elric 	*mechanisms = GSS_C_NO_OID_SET;
     81  1.1     elric 
     82  1.1     elric     if (cred_handle == GSS_C_NO_CREDENTIAL)
     83  1.1     elric 	return GSS_S_NO_CRED;
     84  1.1     elric 
     85  1.1     elric     if (mechanisms) {
     86  1.1     elric         ret = gss_create_empty_oid_set(minor_status, mechanisms);
     87  1.1     elric         if (ret)
     88  1.1     elric 	    goto out;
     89  1.1     elric 	ret = gss_add_oid_set_member(minor_status,
     90  1.1     elric 				     GSS_NTLM_MECHANISM,
     91  1.1     elric 				     mechanisms);
     92  1.1     elric         if (ret)
     93  1.1     elric 	    goto out;
     94  1.1     elric     }
     95  1.1     elric 
     96  1.1     elric     return GSS_S_COMPLETE;
     97  1.1     elric out:
     98  1.1     elric     gss_release_oid_set(&junk, mechanisms);
     99  1.1     elric     return ret;
    100  1.1     elric }
    101  1.1     elric 
    102  1.2  christos #ifdef HAVE_KCM
    103  1.2  christos static OM_uint32
    104  1.2  christos _gss_ntlm_destroy_kcm_cred(gss_cred_id_t *cred_handle)
    105  1.1     elric {
    106  1.1     elric     krb5_storage *request, *response;
    107  1.1     elric     krb5_data response_data;
    108  1.1     elric     krb5_context context;
    109  1.2  christos     krb5_error_code ret;
    110  1.1     elric     ntlm_cred cred;
    111  1.1     elric 
    112  1.1     elric     cred = (ntlm_cred)*cred_handle;
    113  1.1     elric 
    114  1.1     elric     ret = krb5_init_context(&context);
    115  1.2  christos     if (ret)
    116  1.2  christos         return ret;
    117  1.1     elric 
    118  1.1     elric     ret = krb5_kcm_storage_request(context, KCM_OP_DEL_NTLM_CRED, &request);
    119  1.1     elric     if (ret)
    120  1.1     elric 	goto out;
    121  1.1     elric 
    122  1.1     elric     ret = krb5_store_stringz(request, cred->username);
    123  1.1     elric     if (ret)
    124  1.1     elric 	goto out;
    125  1.1     elric 
    126  1.1     elric     ret = krb5_store_stringz(request, cred->domain);
    127  1.1     elric     if (ret)
    128  1.1     elric 	goto out;
    129  1.1     elric 
    130  1.1     elric     ret = krb5_kcm_call(context, request, &response, &response_data);
    131  1.1     elric     if (ret)
    132  1.1     elric 	goto out;
    133  1.1     elric 
    134  1.1     elric     krb5_storage_free(request);
    135  1.1     elric     krb5_storage_free(response);
    136  1.1     elric     krb5_data_free(&response_data);
    137  1.1     elric 
    138  1.1     elric  out:
    139  1.1     elric     krb5_free_context(context);
    140  1.2  christos 
    141  1.2  christos     return ret;
    142  1.2  christos }
    143  1.2  christos #endif /* HAVE_KCM */
    144  1.2  christos 
    145  1.2  christos OM_uint32 GSSAPI_CALLCONV
    146  1.2  christos _gss_ntlm_destroy_cred(OM_uint32 *minor_status,
    147  1.2  christos 		       gss_cred_id_t *cred_handle)
    148  1.2  christos {
    149  1.2  christos #ifdef HAVE_KCM
    150  1.2  christos     krb5_error_code ret;
    151  1.2  christos #endif
    152  1.2  christos 
    153  1.2  christos     if (cred_handle == NULL || *cred_handle == GSS_C_NO_CREDENTIAL)
    154  1.2  christos 	return GSS_S_COMPLETE;
    155  1.2  christos 
    156  1.2  christos #ifdef HAVE_KCM
    157  1.2  christos     ret = _gss_ntlm_destroy_kcm_cred(cred_handle);
    158  1.1     elric     if (ret) {
    159  1.1     elric 	*minor_status = ret;
    160  1.1     elric 	return GSS_S_FAILURE;
    161  1.1     elric     }
    162  1.2  christos #endif
    163  1.1     elric 
    164  1.1     elric     return _gss_ntlm_release_cred(minor_status, cred_handle);
    165  1.1     elric }
    166