Home | History | Annotate | Line # | Download | only in kcm
client.c revision 1.1
      1  1.1  elric /*	$NetBSD: client.c,v 1.1 2011/04/13 18:14:36 elric Exp $	*/
      2  1.1  elric 
      3  1.1  elric /*
      4  1.1  elric  * Copyright (c) 2005, PADL Software Pty Ltd.
      5  1.1  elric  * All rights reserved.
      6  1.1  elric  *
      7  1.1  elric  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
      8  1.1  elric  *
      9  1.1  elric  * Redistribution and use in source and binary forms, with or without
     10  1.1  elric  * modification, are permitted provided that the following conditions
     11  1.1  elric  * are met:
     12  1.1  elric  *
     13  1.1  elric  * 1. Redistributions of source code must retain the above copyright
     14  1.1  elric  *    notice, this list of conditions and the following disclaimer.
     15  1.1  elric  *
     16  1.1  elric  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  elric  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  elric  *    documentation and/or other materials provided with the distribution.
     19  1.1  elric  *
     20  1.1  elric  * 3. Neither the name of PADL Software nor the names of its contributors
     21  1.1  elric  *    may be used to endorse or promote products derived from this software
     22  1.1  elric  *    without specific prior written permission.
     23  1.1  elric  *
     24  1.1  elric  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  elric  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
     28  1.1  elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  elric  * SUCH DAMAGE.
     35  1.1  elric  */
     36  1.1  elric 
     37  1.1  elric #include "kcm_locl.h"
     38  1.1  elric #include <pwd.h>
     39  1.1  elric 
     40  1.1  elric krb5_error_code
     41  1.1  elric kcm_ccache_resolve_client(krb5_context context,
     42  1.1  elric 			  kcm_client *client,
     43  1.1  elric 			  kcm_operation opcode,
     44  1.1  elric 			  const char *name,
     45  1.1  elric 			  kcm_ccache *ccache)
     46  1.1  elric {
     47  1.1  elric     krb5_error_code ret;
     48  1.1  elric 
     49  1.1  elric     ret = kcm_ccache_resolve(context, name, ccache);
     50  1.1  elric     if (ret) {
     51  1.1  elric 	kcm_log(1, "Failed to resolve cache %s: %s",
     52  1.1  elric 		name, krb5_get_err_text(context, ret));
     53  1.1  elric 	return ret;
     54  1.1  elric     }
     55  1.1  elric 
     56  1.1  elric     ret = kcm_access(context, client, opcode, *ccache);
     57  1.1  elric     if (ret) {
     58  1.1  elric 	ret = KRB5_FCC_NOFILE; /* don't disclose */
     59  1.1  elric 	kcm_release_ccache(context, *ccache);
     60  1.1  elric     }
     61  1.1  elric 
     62  1.1  elric     return ret;
     63  1.1  elric }
     64  1.1  elric 
     65  1.1  elric krb5_error_code
     66  1.1  elric kcm_ccache_destroy_client(krb5_context context,
     67  1.1  elric 			  kcm_client *client,
     68  1.1  elric 			  const char *name)
     69  1.1  elric {
     70  1.1  elric     krb5_error_code ret;
     71  1.1  elric     kcm_ccache ccache;
     72  1.1  elric 
     73  1.1  elric     ret = kcm_ccache_resolve(context, name, &ccache);
     74  1.1  elric     if (ret) {
     75  1.1  elric 	kcm_log(1, "Failed to resolve cache %s: %s",
     76  1.1  elric 		name, krb5_get_err_text(context, ret));
     77  1.1  elric 	return ret;
     78  1.1  elric     }
     79  1.1  elric 
     80  1.1  elric     ret = kcm_access(context, client, KCM_OP_DESTROY, ccache);
     81  1.1  elric     kcm_cleanup_events(context, ccache);
     82  1.1  elric     kcm_release_ccache(context, ccache);
     83  1.1  elric     if (ret)
     84  1.1  elric 	return ret;
     85  1.1  elric 
     86  1.1  elric     return kcm_ccache_destroy(context, name);
     87  1.1  elric }
     88  1.1  elric 
     89  1.1  elric krb5_error_code
     90  1.1  elric kcm_ccache_new_client(krb5_context context,
     91  1.1  elric 		      kcm_client *client,
     92  1.1  elric 		      const char *name,
     93  1.1  elric 		      kcm_ccache *ccache_p)
     94  1.1  elric {
     95  1.1  elric     krb5_error_code ret;
     96  1.1  elric     kcm_ccache ccache;
     97  1.1  elric 
     98  1.1  elric     /* We insist the ccache name starts with UID or UID: */
     99  1.1  elric     if (name_constraints != 0) {
    100  1.1  elric 	char prefix[64];
    101  1.1  elric 	size_t prefix_len;
    102  1.1  elric 	int bad = 1;
    103  1.1  elric 
    104  1.1  elric 	snprintf(prefix, sizeof(prefix), "%ld:", (long)client->uid);
    105  1.1  elric 	prefix_len = strlen(prefix);
    106  1.1  elric 
    107  1.1  elric 	if (strncmp(name, prefix, prefix_len) == 0)
    108  1.1  elric 	    bad = 0;
    109  1.1  elric 	else {
    110  1.1  elric 	    prefix[prefix_len - 1] = '\0';
    111  1.1  elric 	    if (strcmp(name, prefix) == 0)
    112  1.1  elric 		bad = 0;
    113  1.1  elric 	}
    114  1.1  elric 
    115  1.1  elric 	/* Allow root to create badly-named ccaches */
    116  1.1  elric 	if (bad && !CLIENT_IS_ROOT(client))
    117  1.1  elric 	    return KRB5_CC_BADNAME;
    118  1.1  elric     }
    119  1.1  elric 
    120  1.1  elric     ret = kcm_ccache_resolve(context, name, &ccache);
    121  1.1  elric     if (ret == 0) {
    122  1.1  elric 	if ((ccache->uid != client->uid ||
    123  1.1  elric 	     ccache->gid != client->gid) && !CLIENT_IS_ROOT(client))
    124  1.1  elric 	    return KRB5_FCC_PERM;
    125  1.1  elric     } else if (ret != KRB5_FCC_NOFILE && !(CLIENT_IS_ROOT(client) && ret == KRB5_FCC_PERM)) {
    126  1.1  elric 		return ret;
    127  1.1  elric     }
    128  1.1  elric 
    129  1.1  elric     if (ret == KRB5_FCC_NOFILE) {
    130  1.1  elric 	ret = kcm_ccache_new(context, name, &ccache);
    131  1.1  elric 	if (ret) {
    132  1.1  elric 	    kcm_log(1, "Failed to initialize cache %s: %s",
    133  1.1  elric 		    name, krb5_get_err_text(context, ret));
    134  1.1  elric 	    return ret;
    135  1.1  elric 	}
    136  1.1  elric 
    137  1.1  elric 	/* bind to current client */
    138  1.1  elric 	ccache->uid = client->uid;
    139  1.1  elric 	ccache->gid = client->gid;
    140  1.1  elric 	ccache->session = client->session;
    141  1.1  elric     } else {
    142  1.1  elric 	ret = kcm_zero_ccache_data(context, ccache);
    143  1.1  elric 	if (ret) {
    144  1.1  elric 	    kcm_log(1, "Failed to empty cache %s: %s",
    145  1.1  elric 		    name, krb5_get_err_text(context, ret));
    146  1.1  elric 	    kcm_release_ccache(context, ccache);
    147  1.1  elric 	    return ret;
    148  1.1  elric 	}
    149  1.1  elric 	kcm_cleanup_events(context, ccache);
    150  1.1  elric     }
    151  1.1  elric 
    152  1.1  elric     ret = kcm_access(context, client, KCM_OP_INITIALIZE, ccache);
    153  1.1  elric     if (ret) {
    154  1.1  elric 	kcm_release_ccache(context, ccache);
    155  1.1  elric 	kcm_ccache_destroy(context, name);
    156  1.1  elric 	return ret;
    157  1.1  elric     }
    158  1.1  elric 
    159  1.1  elric     /*
    160  1.1  elric      * Finally, if the user is root and the cache was created under
    161  1.1  elric      * another user's name, chown the cache to that user and their
    162  1.1  elric      * default gid.
    163  1.1  elric      */
    164  1.1  elric     if (CLIENT_IS_ROOT(client)) {
    165  1.1  elric 	unsigned long uid;
    166  1.1  elric 	int matches = sscanf(name,"%ld:",&uid);
    167  1.1  elric 	if (matches == 0)
    168  1.1  elric 	    matches = sscanf(name,"%ld",&uid);
    169  1.1  elric 	if (matches == 1) {
    170  1.1  elric 	    struct passwd *pwd = getpwuid(uid);
    171  1.1  elric 	    if (pwd != NULL) {
    172  1.1  elric 		gid_t gid = pwd->pw_gid;
    173  1.1  elric 		kcm_chown(context, client, ccache, uid, gid);
    174  1.1  elric 	    }
    175  1.1  elric 	}
    176  1.1  elric     }
    177  1.1  elric 
    178  1.1  elric     *ccache_p = ccache;
    179  1.1  elric     return 0;
    180  1.1  elric }
    181  1.1  elric 
    182