Home | History | Annotate | Line # | Download | only in krb5
kuserok.c revision 1.2
      1  1.1     elric /*	$NetBSD: kuserok.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 1997 - 2005 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  * Redistribution and use in source and binary forms, with or without
      9  1.1     elric  * modification, are permitted provided that the following conditions
     10  1.1     elric  * are met:
     11  1.1     elric  *
     12  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     14  1.1     elric  *
     15  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     elric  *    documentation and/or other materials provided with the distribution.
     18  1.1     elric  *
     19  1.1     elric  * 3. Neither the name of the Institute nor the names of its contributors
     20  1.1     elric  *    may be used to endorse or promote products derived from this software
     21  1.1     elric  *    without specific prior written permission.
     22  1.1     elric  *
     23  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     24  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     27  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1     elric  * SUCH DAMAGE.
     34  1.1     elric  */
     35  1.1     elric 
     36  1.1     elric #include "krb5_locl.h"
     37  1.2  christos #include "kuserok_plugin.h"
     38  1.1     elric #include <dirent.h>
     39  1.1     elric 
     40  1.2  christos #ifndef SYSTEM_K5LOGIN_DIR
     41  1.2  christos /*
     42  1.2  christos  * System k5login location.  File namess in this directory are expected
     43  1.2  christos  * to be usernames and to contain a list of principals allowed to login
     44  1.2  christos  * as the user named the same as the file.
     45  1.2  christos  */
     46  1.2  christos #define SYSTEM_K5LOGIN_DIR SYSCONFDIR "/k5login.d"
     47  1.2  christos #endif
     48  1.2  christos 
     49  1.2  christos /* Plugin framework bits */
     50  1.2  christos 
     51  1.2  christos struct plctx {
     52  1.2  christos     const char           *rule;
     53  1.2  christos     const char           *k5login_dir;
     54  1.2  christos     const char           *luser;
     55  1.2  christos     krb5_const_principal principal;
     56  1.2  christos     unsigned int         flags;
     57  1.2  christos     krb5_boolean         result;
     58  1.2  christos };
     59  1.2  christos 
     60  1.2  christos static krb5_error_code KRB5_LIB_CALL
     61  1.2  christos plcallback(krb5_context context, const void *plug, void *plugctx, void *userctx)
     62  1.2  christos {
     63  1.2  christos     const krb5plugin_kuserok_ftable *locate = plug;
     64  1.2  christos     struct plctx *plctx = userctx;
     65  1.2  christos 
     66  1.2  christos     return locate->kuserok(plugctx, context, plctx->rule, plctx->flags,
     67  1.2  christos 			   plctx->k5login_dir, plctx->luser, plctx->principal,
     68  1.2  christos 			   &plctx->result);
     69  1.2  christos }
     70  1.2  christos 
     71  1.2  christos static krb5_error_code plugin_reg_ret;
     72  1.2  christos static krb5plugin_kuserok_ftable kuserok_simple_plug;
     73  1.2  christos static krb5plugin_kuserok_ftable kuserok_sys_k5login_plug;
     74  1.2  christos static krb5plugin_kuserok_ftable kuserok_user_k5login_plug;
     75  1.2  christos static krb5plugin_kuserok_ftable kuserok_deny_plug;
     76  1.2  christos 
     77  1.2  christos static void
     78  1.2  christos reg_def_plugins_once(void *ctx)
     79  1.2  christos {
     80  1.2  christos     krb5_error_code ret;
     81  1.2  christos     krb5_context context = ctx;
     82  1.2  christos 
     83  1.2  christos     plugin_reg_ret = krb5_plugin_register(context, PLUGIN_TYPE_DATA,
     84  1.2  christos 					  KRB5_PLUGIN_KUSEROK,
     85  1.2  christos 					  &kuserok_simple_plug);
     86  1.2  christos     ret = krb5_plugin_register(context, PLUGIN_TYPE_DATA,
     87  1.2  christos                                KRB5_PLUGIN_KUSEROK, &kuserok_sys_k5login_plug);
     88  1.2  christos     if (!plugin_reg_ret)
     89  1.2  christos 	plugin_reg_ret = ret;
     90  1.2  christos     ret = krb5_plugin_register(context, PLUGIN_TYPE_DATA,
     91  1.2  christos                                KRB5_PLUGIN_KUSEROK, &kuserok_user_k5login_plug);
     92  1.2  christos     if (!plugin_reg_ret)
     93  1.2  christos 	plugin_reg_ret = ret;
     94  1.2  christos     ret = krb5_plugin_register(context, PLUGIN_TYPE_DATA,
     95  1.2  christos                                KRB5_PLUGIN_KUSEROK, &kuserok_deny_plug);
     96  1.2  christos     if (!plugin_reg_ret)
     97  1.2  christos 	plugin_reg_ret = ret;
     98  1.2  christos }
     99  1.2  christos 
    100  1.2  christos /**
    101  1.2  christos  * This function is designed to be portable for Win32 and POSIX.  The
    102  1.2  christos  * design does lead to multiple getpwnam_r() calls, but this is probably
    103  1.2  christos  * not a big deal.
    104  1.2  christos  *
    105  1.2  christos  * Inputs:
    106  1.2  christos  *
    107  1.2  christos  * @param context            A krb5_context
    108  1.2  christos  * @param filename	     Name of item to introspection
    109  1.2  christos  * @param is_system_location TRUE if the dir/file are system locations or
    110  1.2  christos  *                     	     FALSE if they are user home directory locations
    111  1.2  christos  * @param dir                Directory (optional)
    112  1.2  christos  * @param dirlstat           A pointer to struct stat for the directory (optional)
    113  1.2  christos  * @param file               File (optional)
    114  1.2  christos  * @param owner              Name of user that is expected to own the file
    115  1.2  christos  */
    116  1.2  christos 
    117  1.2  christos static krb5_error_code
    118  1.2  christos check_owner_dir(krb5_context context,
    119  1.2  christos 		const char *filename,
    120  1.2  christos 		krb5_boolean is_system_location,
    121  1.2  christos 		DIR *dir,
    122  1.2  christos 		struct stat *dirlstat,
    123  1.2  christos 		const char *owner)
    124  1.2  christos {
    125  1.2  christos #ifdef _WIN32
    126  1.2  christos     /*
    127  1.2  christos      * XXX Implement this!
    128  1.2  christos      *
    129  1.2  christos      * The thing to do is to call _get_osfhandle() on fileno(file) and
    130  1.2  christos      * dirfd(dir) to get HANDLEs to the same, then call
    131  1.2  christos      * GetSecurityInfo() on those HANDLEs to get the security descriptor
    132  1.2  christos      * (SD), then check the owner and DACL.  Checking the DACL sounds
    133  1.2  christos      * like a lot of work (what, derive a mode from the ACL the way
    134  1.2  christos      * NFSv4 servers do?).  Checking the owner means doing an LSARPC
    135  1.2  christos      * lookup at least (to get the user's SID).
    136  1.2  christos      */
    137  1.2  christos     if (is_system_location || owner == NULL)
    138  1.2  christos 	return 0;
    139  1.2  christos     krb5_set_error_message(context, EACCES,
    140  1.2  christos 			   "User k5login files not supported on Windows");
    141  1.2  christos     return EACCES;
    142  1.2  christos #else
    143  1.2  christos     struct passwd pw, *pwd = NULL;
    144  1.2  christos     char pwbuf[2048];
    145  1.2  christos     struct stat st;
    146  1.2  christos 
    147  1.2  christos     heim_assert(owner != NULL, "no directory owner ?");
    148  1.2  christos 
    149  1.2  christos     if (rk_getpwnam_r(owner, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0) {
    150  1.2  christos 	krb5_set_error_message(context, errno,
    151  1.2  christos 			       "User unknown %s (getpwnam_r())", owner);
    152  1.2  christos 	return EACCES;
    153  1.2  christos     }
    154  1.2  christos     if (pwd == NULL) {
    155  1.2  christos 	krb5_set_error_message(context, EACCES, "no user %s", owner);
    156  1.2  christos 	return EACCES;
    157  1.2  christos     }
    158  1.2  christos 
    159  1.2  christos     if (fstat(dirfd(dir), &st) == -1) {
    160  1.2  christos 	krb5_set_error_message(context, EACCES,
    161  1.2  christos 			       "fstat(%s) of k5login.d failed",
    162  1.2  christos 			       filename);
    163  1.2  christos 	return EACCES;
    164  1.2  christos     }
    165  1.2  christos     if (!S_ISDIR(st.st_mode)) {
    166  1.2  christos 	krb5_set_error_message(context, ENOTDIR, "%s not a directory",
    167  1.2  christos 			       filename);
    168  1.2  christos 	return ENOTDIR;
    169  1.2  christos     }
    170  1.2  christos     if (st.st_dev != dirlstat->st_dev || st.st_ino != dirlstat->st_ino) {
    171  1.2  christos 	krb5_set_error_message(context, EACCES,
    172  1.2  christos 			       "%s was renamed during kuserok "
    173  1.2  christos 			       "operation", filename);
    174  1.2  christos 	return EACCES;
    175  1.2  christos     }
    176  1.2  christos     if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
    177  1.2  christos 	krb5_set_error_message(context, EACCES,
    178  1.2  christos 			       "%s has world and/or group write "
    179  1.2  christos 			       "permissions", filename);
    180  1.2  christos 	return EACCES;
    181  1.2  christos     }
    182  1.2  christos     if (pwd->pw_uid != st.st_uid && st.st_uid != 0) {
    183  1.2  christos 	krb5_set_error_message(context, EACCES,
    184  1.2  christos 			       "%s not owned by the user (%s) or root",
    185  1.2  christos 			       filename, owner);
    186  1.2  christos 	return EACCES;
    187  1.2  christos     }
    188  1.2  christos 
    189  1.2  christos     return 0;
    190  1.2  christos #endif
    191  1.2  christos }
    192  1.2  christos 
    193  1.2  christos static krb5_error_code
    194  1.2  christos check_owner_file(krb5_context context,
    195  1.2  christos 		 const char *filename,
    196  1.2  christos 		 FILE *file, const char *owner)
    197  1.2  christos {
    198  1.2  christos #ifdef _WIN32
    199  1.2  christos     /*
    200  1.2  christos      * XXX Implement this!
    201  1.2  christos      *
    202  1.2  christos      * The thing to do is to call _get_osfhandle() on fileno(file) and
    203  1.2  christos      * dirfd(dir) to get HANDLEs to the same, then call
    204  1.2  christos      * GetSecurityInfo() on those HANDLEs to get the security descriptor
    205  1.2  christos      * (SD), then check the owner and DACL.  Checking the DACL sounds
    206  1.2  christos      * like a lot of work (what, derive a mode from the ACL the way
    207  1.2  christos      * NFSv4 servers do?).  Checking the owner means doing an LSARPC
    208  1.2  christos      * lookup at least (to get the user's SID).
    209  1.2  christos      */
    210  1.2  christos     if (owner == NULL)
    211  1.2  christos 	return 0;
    212  1.2  christos 
    213  1.2  christos     krb5_set_error_message(context, EACCES,
    214  1.2  christos 			   "User k5login files not supported on Windows");
    215  1.2  christos     return EACCES;
    216  1.2  christos #else
    217  1.2  christos     struct passwd pw, *pwd = NULL;
    218  1.2  christos     char pwbuf[2048];
    219  1.2  christos     struct stat st;
    220  1.2  christos 
    221  1.2  christos     if (owner == NULL)
    222  1.2  christos 	return 0;
    223  1.2  christos 
    224  1.2  christos     if (rk_getpwnam_r(owner, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0) {
    225  1.2  christos 	krb5_set_error_message(context, errno,
    226  1.2  christos 			       "User unknown %s (getpwnam_r())", owner);
    227  1.2  christos 	return EACCES;
    228  1.2  christos     }
    229  1.2  christos     if (pwd == NULL) {
    230  1.2  christos 	krb5_set_error_message(context, EACCES, "no user %s", owner);
    231  1.2  christos 	return EACCES;
    232  1.2  christos     }
    233  1.2  christos 
    234  1.2  christos     if (fstat(fileno(file), &st) == -1) {
    235  1.2  christos 	krb5_set_error_message(context, EACCES, "fstat(%s) of k5login failed",
    236  1.2  christos 			       filename);
    237  1.2  christos 	return EACCES;
    238  1.2  christos     }
    239  1.2  christos     if (S_ISDIR(st.st_mode)) {
    240  1.2  christos 	krb5_set_error_message(context, EISDIR, "k5login: %s is a directory",
    241  1.2  christos 			       filename);
    242  1.2  christos 	return EISDIR;
    243  1.2  christos     }
    244  1.2  christos     if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
    245  1.2  christos 	krb5_set_error_message(context, EISDIR,
    246  1.2  christos 			       "k5login %s has world and/or group write "
    247  1.2  christos 			       "permissions", filename);
    248  1.2  christos 	return EACCES;
    249  1.2  christos     }
    250  1.2  christos     if (pwd->pw_uid != st.st_uid && st.st_uid != 0) {
    251  1.2  christos 	krb5_set_error_message(context, EACCES,
    252  1.2  christos 			       "k5login %s not owned by the user or root",
    253  1.2  christos 			       filename);
    254  1.2  christos 	return EACCES;
    255  1.2  christos     }
    256  1.2  christos 
    257  1.2  christos     return 0;
    258  1.2  christos #endif
    259  1.2  christos }
    260  1.2  christos 
    261  1.1     elric 
    262  1.1     elric /* see if principal is mentioned in the filename access file, return
    263  1.1     elric    TRUE (in result) if so, FALSE otherwise */
    264  1.1     elric 
    265  1.1     elric static krb5_error_code
    266  1.1     elric check_one_file(krb5_context context,
    267  1.1     elric 	       const char *filename,
    268  1.2  christos 	       const char *owner,
    269  1.2  christos 	       krb5_boolean is_system_location,
    270  1.2  christos 	       krb5_const_principal principal,
    271  1.1     elric 	       krb5_boolean *result)
    272  1.1     elric {
    273  1.1     elric     FILE *f;
    274  1.1     elric     char buf[BUFSIZ];
    275  1.1     elric     krb5_error_code ret;
    276  1.1     elric 
    277  1.1     elric     *result = FALSE;
    278  1.1     elric 
    279  1.2  christos     f = fopen(filename, "r");
    280  1.1     elric     if (f == NULL)
    281  1.1     elric 	return errno;
    282  1.1     elric     rk_cloexec_file(f);
    283  1.1     elric 
    284  1.2  christos     ret = check_owner_file(context, filename, f, owner);
    285  1.2  christos     if (ret)
    286  1.2  christos 	goto out;
    287  1.1     elric 
    288  1.2  christos     while (fgets(buf, sizeof(buf), f) != NULL) {
    289  1.1     elric 	krb5_principal tmp;
    290  1.1     elric 	char *newline = buf + strcspn(buf, "\n");
    291  1.1     elric 
    292  1.2  christos 	if (*newline != '\n') {
    293  1.1     elric 	    int c;
    294  1.1     elric 	    c = fgetc(f);
    295  1.2  christos 	    if (c != EOF) {
    296  1.2  christos 		while (c != EOF && c != '\n')
    297  1.1     elric 		    c = fgetc(f);
    298  1.1     elric 		/* line was too long, so ignore it */
    299  1.1     elric 		continue;
    300  1.1     elric 	    }
    301  1.1     elric 	}
    302  1.1     elric 	*newline = '\0';
    303  1.2  christos 	ret = krb5_parse_name(context, buf, &tmp);
    304  1.1     elric 	if (ret)
    305  1.1     elric 	    continue;
    306  1.2  christos 	*result = krb5_principal_compare(context, principal, tmp);
    307  1.2  christos 	krb5_free_principal(context, tmp);
    308  1.1     elric 	if (*result) {
    309  1.1     elric 	    fclose (f);
    310  1.1     elric 	    return 0;
    311  1.1     elric 	}
    312  1.1     elric     }
    313  1.2  christos 
    314  1.2  christos out:
    315  1.2  christos     fclose(f);
    316  1.1     elric     return 0;
    317  1.1     elric }
    318  1.1     elric 
    319  1.1     elric static krb5_error_code
    320  1.1     elric check_directory(krb5_context context,
    321  1.1     elric 		const char *dirname,
    322  1.2  christos 		const char *owner,
    323  1.2  christos 	        krb5_boolean is_system_location,
    324  1.2  christos 		krb5_const_principal principal,
    325  1.1     elric 		krb5_boolean *result)
    326  1.1     elric {
    327  1.1     elric     DIR *d;
    328  1.1     elric     struct dirent *dent;
    329  1.1     elric     char filename[MAXPATHLEN];
    330  1.2  christos     size_t len;
    331  1.1     elric     krb5_error_code ret = 0;
    332  1.1     elric     struct stat st;
    333  1.1     elric 
    334  1.1     elric     *result = FALSE;
    335  1.1     elric 
    336  1.2  christos     if (lstat(dirname, &st) < 0)
    337  1.1     elric 	return errno;
    338  1.1     elric 
    339  1.2  christos     if (!S_ISDIR(st.st_mode)) {
    340  1.2  christos 	krb5_set_error_message(context, ENOTDIR, "k5login.d not a directory");
    341  1.1     elric 	return ENOTDIR;
    342  1.2  christos     }
    343  1.1     elric 
    344  1.2  christos     if ((d = opendir(dirname)) == NULL) {
    345  1.2  christos 	krb5_set_error_message(context, ENOTDIR, "Could not open k5login.d");
    346  1.1     elric 	return errno;
    347  1.2  christos     }
    348  1.1     elric 
    349  1.2  christos     ret = check_owner_dir(context, dirname, is_system_location, d, &st, owner);
    350  1.2  christos     if (ret)
    351  1.2  christos 	goto out;
    352  1.1     elric 
    353  1.2  christos     while ((dent = readdir(d)) != NULL) {
    354  1.2  christos 	/*
    355  1.2  christos 	 * XXX: Should we also skip files whose names start with "."?
    356  1.2  christos 	 * Vim ".filename.swp" files are also good candidates to skip.
    357  1.2  christos 	 * Once we ignore "#*" and "*~", it is not clear what other
    358  1.2  christos 	 * heuristics to apply.
    359  1.2  christos 	 */
    360  1.2  christos 	if (strcmp(dent->d_name, ".") == 0 ||
    361  1.1     elric 	   strcmp(dent->d_name, "..") == 0 ||
    362  1.1     elric 	   dent->d_name[0] == '#' ||			  /* emacs autosave */
    363  1.1     elric 	   dent->d_name[strlen(dent->d_name) - 1] == '~') /* emacs backup */
    364  1.1     elric 	    continue;
    365  1.2  christos 	len = snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
    366  1.2  christos 	/* Skip too-long filenames that got truncated by snprintf() */
    367  1.2  christos 	if (len < sizeof(filename)) {
    368  1.2  christos 	    ret = check_one_file(context, filename, owner, is_system_location,
    369  1.2  christos 				 principal, result);
    370  1.2  christos 	    if (ret == 0 && *result == TRUE)
    371  1.2  christos 		break;
    372  1.2  christos 	}
    373  1.1     elric 	ret = 0; /* don't propagate errors upstream */
    374  1.1     elric     }
    375  1.2  christos 
    376  1.2  christos out:
    377  1.1     elric     closedir(d);
    378  1.1     elric     return ret;
    379  1.1     elric }
    380  1.1     elric 
    381  1.2  christos static krb5_error_code
    382  1.2  christos check_an2ln(krb5_context context,
    383  1.2  christos 	    krb5_const_principal principal,
    384  1.2  christos 	    const char *luser,
    385  1.2  christos 	    krb5_boolean *result)
    386  1.1     elric {
    387  1.1     elric     krb5_error_code ret;
    388  1.2  christos     char *lname;
    389  1.1     elric 
    390  1.2  christos #if 0
    391  1.2  christos     /* XXX Should we make this an option? */
    392  1.1     elric     /* multi-component principals can never match */
    393  1.2  christos     if (krb5_principal_get_comp_string(context, principal, 1) != NULL) {
    394  1.2  christos 	*result =  FALSE;
    395  1.2  christos 	return 0;
    396  1.2  christos     }
    397  1.2  christos #endif
    398  1.1     elric 
    399  1.2  christos     lname = malloc(strlen(luser) + 1);
    400  1.2  christos     if (lname == NULL)
    401  1.2  christos 	return krb5_enomem(context);
    402  1.2  christos     ret = krb5_aname_to_localname(context, principal, strlen(luser)+1, lname);
    403  1.1     elric     if (ret)
    404  1.2  christos 	goto out;
    405  1.2  christos     if (strcmp(lname, luser) == 0)
    406  1.2  christos 	*result = TRUE;
    407  1.2  christos     else
    408  1.2  christos 	*result = FALSE;
    409  1.2  christos 
    410  1.2  christos out:
    411  1.2  christos     free(lname);
    412  1.2  christos     return 0;
    413  1.2  christos 
    414  1.1     elric }
    415  1.1     elric 
    416  1.1     elric /**
    417  1.1     elric  * This function takes the name of a local user and checks if
    418  1.1     elric  * principal is allowed to log in as that user.
    419  1.1     elric  *
    420  1.1     elric  * The user may have a ~/.k5login file listing principals that are
    421  1.1     elric  * allowed to login as that user. If that file does not exist, all
    422  1.2  christos  * principals with a only one component that is identical to the
    423  1.2  christos  * username, and a realm considered local, are allowed access.
    424  1.1     elric  *
    425  1.1     elric  * The .k5login file must contain one principal per line, be owned by
    426  1.1     elric  * user and not be writable by group or other (but must be readable by
    427  1.1     elric  * anyone).
    428  1.1     elric  *
    429  1.1     elric  * Note that if the file exists, no implicit access rights are given
    430  1.1     elric  * to user@@LOCALREALM.
    431  1.1     elric  *
    432  1.1     elric  * Optionally, a set of files may be put in ~/.k5login.d (a
    433  1.1     elric  * directory), in which case they will all be checked in the same
    434  1.1     elric  * manner as .k5login.  The files may be called anything, but files
    435  1.1     elric  * starting with a hash (#) , or ending with a tilde (~) are
    436  1.1     elric  * ignored. Subdirectories are not traversed. Note that this directory
    437  1.1     elric  * may not be checked by other Kerberos implementations.
    438  1.1     elric  *
    439  1.1     elric  * If no configuration file exists, match user against local domains,
    440  1.1     elric  * ie luser@@LOCAL-REALMS-IN-CONFIGURATION-FILES.
    441  1.1     elric  *
    442  1.1     elric  * @param context Kerberos 5 context.
    443  1.1     elric  * @param principal principal to check if allowed to login
    444  1.1     elric  * @param luser local user id
    445  1.2  christos  *
    446  1.1     elric  * @return returns TRUE if access should be granted, FALSE otherwise.
    447  1.1     elric  *
    448  1.1     elric  * @ingroup krb5_support
    449  1.1     elric  */
    450  1.1     elric 
    451  1.1     elric KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
    452  1.2  christos krb5_kuserok(krb5_context context,
    453  1.2  christos 	     krb5_principal principal,
    454  1.2  christos 	     const char *luser)
    455  1.2  christos {
    456  1.2  christos     return _krb5_kuserok(context, principal, luser, TRUE);
    457  1.2  christos }
    458  1.2  christos 
    459  1.2  christos 
    460  1.2  christos KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
    461  1.2  christos _krb5_kuserok(krb5_context context,
    462  1.1     elric 	      krb5_principal principal,
    463  1.2  christos 	      const char *luser,
    464  1.2  christos 	      krb5_boolean an2ln_ok)
    465  1.2  christos {
    466  1.2  christos     static heim_base_once_t reg_def_plugins = HEIM_BASE_ONCE_INIT;
    467  1.2  christos     krb5_error_code ret;
    468  1.2  christos     struct plctx ctx;
    469  1.2  christos     char **rules;
    470  1.2  christos 
    471  1.2  christos     /*
    472  1.2  christos      * XXX we should have a struct with a krb5_context field and a
    473  1.2  christos      * krb5_error_code fied and pass the address of that as the ctx
    474  1.2  christos      * argument of heim_base_once_f().  For now we use a static to
    475  1.2  christos      * communicate failures.  Actually, we ignore failures anyways,
    476  1.2  christos      * since we can't return them.
    477  1.2  christos      */
    478  1.2  christos     heim_base_once_f(&reg_def_plugins, context, reg_def_plugins_once);
    479  1.2  christos 
    480  1.2  christos     ctx.flags = 0;
    481  1.2  christos     ctx.luser = luser;
    482  1.2  christos     ctx.principal = principal;
    483  1.2  christos     ctx.result = FALSE;
    484  1.2  christos 
    485  1.2  christos     ctx.k5login_dir = krb5_config_get_string(context, NULL, "libdefaults",
    486  1.2  christos 					     "k5login_directory", NULL);
    487  1.2  christos 
    488  1.2  christos     if (an2ln_ok)
    489  1.2  christos 	ctx.flags |= KUSEROK_ANAME_TO_LNAME_OK;
    490  1.2  christos 
    491  1.2  christos     if (krb5_config_get_bool_default(context, NULL, FALSE, "libdefaults",
    492  1.2  christos 				     "k5login_authoritative", NULL))
    493  1.2  christos 	ctx.flags |= KUSEROK_K5LOGIN_IS_AUTHORITATIVE;
    494  1.2  christos 
    495  1.2  christos     if ((ctx.flags & KUSEROK_K5LOGIN_IS_AUTHORITATIVE) && plugin_reg_ret)
    496  1.2  christos 	return plugin_reg_ret; /* fail safe */
    497  1.2  christos 
    498  1.2  christos     rules = krb5_config_get_strings(context, NULL, "libdefaults",
    499  1.2  christos 				    "kuserok", NULL);
    500  1.2  christos     if (rules == NULL) {
    501  1.2  christos 	/* Default: check ~/.k5login */
    502  1.2  christos 	ctx.rule = "USER-K5LOGIN";
    503  1.2  christos 
    504  1.2  christos 	ret = plcallback(context, &kuserok_user_k5login_plug, NULL, &ctx);
    505  1.2  christos 	if (ret == 0)
    506  1.2  christos 	    goto out;
    507  1.2  christos 
    508  1.2  christos 	ctx.rule = "SIMPLE";
    509  1.2  christos 	ret = plcallback(context, &kuserok_simple_plug, NULL, &ctx);
    510  1.2  christos 	if (ret == 0)
    511  1.2  christos 	    goto out;
    512  1.2  christos 
    513  1.2  christos 	ctx.result = FALSE;
    514  1.2  christos     } else {
    515  1.2  christos 	size_t n;
    516  1.2  christos 
    517  1.2  christos 	for (n = 0; rules[n]; n++) {
    518  1.2  christos 	    ctx.rule = rules[n];
    519  1.2  christos 
    520  1.2  christos 	    ret = _krb5_plugin_run_f(context, "krb5", KRB5_PLUGIN_KUSEROK,
    521  1.2  christos 				     KRB5_PLUGIN_KUSEROK_VERSION_0, 0,
    522  1.2  christos 				     &ctx, plcallback);
    523  1.2  christos 	    if (ret != KRB5_PLUGIN_NO_HANDLE)
    524  1.2  christos 		goto out;
    525  1.2  christos 	}
    526  1.2  christos     }
    527  1.2  christos 
    528  1.2  christos out:
    529  1.2  christos     krb5_config_free_strings(rules);
    530  1.2  christos 
    531  1.2  christos     return ctx.result;
    532  1.2  christos }
    533  1.2  christos 
    534  1.2  christos /*
    535  1.2  christos  * Simple kuserok: check that the lname for the aname matches luser.
    536  1.2  christos  */
    537  1.2  christos 
    538  1.2  christos static krb5_error_code KRB5_LIB_CALL
    539  1.2  christos kuserok_simple_plug_f(void *plug_ctx, krb5_context context, const char *rule,
    540  1.2  christos 		      unsigned int flags, const char *k5login_dir,
    541  1.2  christos 		      const char *luser, krb5_const_principal principal,
    542  1.2  christos 		      krb5_boolean *result)
    543  1.1     elric {
    544  1.1     elric     krb5_error_code ret;
    545  1.1     elric 
    546  1.2  christos     if (strcmp(rule, "SIMPLE") != 0 || (flags & KUSEROK_ANAME_TO_LNAME_OK) == 0)
    547  1.2  christos 	return KRB5_PLUGIN_NO_HANDLE;
    548  1.2  christos 
    549  1.2  christos     ret = check_an2ln(context, principal, luser, result);
    550  1.2  christos     if (ret == 0 && *result == FALSE)
    551  1.2  christos 	return KRB5_PLUGIN_NO_HANDLE;
    552  1.2  christos 
    553  1.2  christos     return 0;
    554  1.2  christos }
    555  1.2  christos 
    556  1.2  christos /*
    557  1.2  christos  * Check k5login files in a system location, rather than in home
    558  1.2  christos  * directories.
    559  1.2  christos  */
    560  1.2  christos 
    561  1.2  christos static krb5_error_code KRB5_LIB_CALL
    562  1.2  christos kuserok_sys_k5login_plug_f(void *plug_ctx, krb5_context context,
    563  1.2  christos 			   const char *rule, unsigned int flags,
    564  1.2  christos 			   const char *k5login_dir, const char *luser,
    565  1.2  christos 			   krb5_const_principal principal, krb5_boolean *result)
    566  1.2  christos {
    567  1.2  christos     char filename[MAXPATHLEN];
    568  1.2  christos     size_t len;
    569  1.2  christos     const char *profile_dir = NULL;
    570  1.2  christos     krb5_error_code ret;
    571  1.2  christos 
    572  1.2  christos     *result = FALSE;
    573  1.2  christos 
    574  1.2  christos     if (strcmp(rule, "SYSTEM-K5LOGIN") != 0 &&
    575  1.2  christos 	strncmp(rule, "SYSTEM-K5LOGIN:", strlen("SYSTEM-K5LOGIN:")) != 0)
    576  1.2  christos 	return KRB5_PLUGIN_NO_HANDLE;
    577  1.2  christos 
    578  1.2  christos     profile_dir = strchr(rule, ':');
    579  1.2  christos     if (profile_dir == NULL)
    580  1.2  christos 	profile_dir = k5login_dir ? k5login_dir : SYSTEM_K5LOGIN_DIR;
    581  1.2  christos     else
    582  1.2  christos 	profile_dir++;
    583  1.2  christos 
    584  1.2  christos     len = snprintf(filename, sizeof(filename), "%s/%s", profile_dir, luser);
    585  1.2  christos     if (len < sizeof(filename)) {
    586  1.2  christos 	ret = check_one_file(context, filename, NULL, TRUE, principal, result);
    587  1.2  christos 
    588  1.2  christos 	if (ret == 0 &&
    589  1.2  christos 	    ((flags & KUSEROK_K5LOGIN_IS_AUTHORITATIVE) || *result == TRUE))
    590  1.2  christos 	    return 0;
    591  1.2  christos     }
    592  1.2  christos 
    593  1.2  christos     *result = FALSE;
    594  1.2  christos     return KRB5_PLUGIN_NO_HANDLE;
    595  1.2  christos }
    596  1.2  christos 
    597  1.2  christos /*
    598  1.2  christos  * Check ~luser/.k5login and/or ~/luser/.k5login.d
    599  1.2  christos  */
    600  1.2  christos 
    601  1.2  christos static krb5_error_code KRB5_LIB_CALL
    602  1.2  christos kuserok_user_k5login_plug_f(void *plug_ctx, krb5_context context,
    603  1.2  christos 			    const char *rule, unsigned int flags,
    604  1.2  christos 			    const char *k5login_dir, const char *luser,
    605  1.2  christos 			    krb5_const_principal principal,
    606  1.2  christos 			    krb5_boolean *result)
    607  1.2  christos {
    608  1.2  christos #ifdef _WIN32
    609  1.2  christos     return KRB5_PLUGIN_NO_HANDLE;
    610  1.2  christos #else
    611  1.2  christos     char *path;
    612  1.2  christos     char *path_exp;
    613  1.2  christos     const char *profile_dir = NULL;
    614  1.2  christos     krb5_error_code ret;
    615  1.1     elric     krb5_boolean found_file = FALSE;
    616  1.2  christos     struct passwd pw, *pwd = NULL;
    617  1.2  christos     char pwbuf[2048];
    618  1.1     elric 
    619  1.2  christos     if (strcmp(rule, "USER-K5LOGIN") != 0)
    620  1.2  christos 	return KRB5_PLUGIN_NO_HANDLE;
    621  1.1     elric 
    622  1.2  christos     profile_dir = k5login_dir;
    623  1.2  christos     if (profile_dir == NULL) {
    624  1.2  christos 	/* Don't deadlock with gssd or anything of the sort */
    625  1.2  christos 	if (!_krb5_homedir_access(context))
    626  1.2  christos 	    return KRB5_PLUGIN_NO_HANDLE;
    627  1.2  christos 
    628  1.2  christos 	if (getpwnam_r(luser, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0) {
    629  1.2  christos 	    krb5_set_error_message(context, errno, "User unknown (getpwnam_r())");
    630  1.2  christos 	    return KRB5_PLUGIN_NO_HANDLE;
    631  1.2  christos 	}
    632  1.2  christos 	if (pwd == NULL) {
    633  1.2  christos 	    krb5_set_error_message(context, errno, "User unknown (getpwnam())");
    634  1.2  christos 	    return KRB5_PLUGIN_NO_HANDLE;
    635  1.2  christos 	}
    636  1.2  christos 	profile_dir = pwd->pw_dir;
    637  1.2  christos     }
    638  1.1     elric 
    639  1.1     elric #define KLOGIN "/.k5login"
    640  1.2  christos 
    641  1.2  christos     if (asprintf(&path, "%s/.k5login.d", profile_dir) == -1)
    642  1.2  christos 	return krb5_enomem(context);
    643  1.2  christos 
    644  1.2  christos     ret = _krb5_expand_path_tokensv(context, path, 1, &path_exp,
    645  1.2  christos 				    "luser", luser, NULL);
    646  1.2  christos     free(path);
    647  1.2  christos     if (ret)
    648  1.2  christos 	return ret;
    649  1.2  christos     path = path_exp;
    650  1.2  christos 
    651  1.1     elric     /* check user's ~/.k5login */
    652  1.2  christos     path[strlen(path) - strlen(".d")] = '\0';
    653  1.2  christos     ret = check_one_file(context, path, luser, FALSE, principal, result);
    654  1.1     elric 
    655  1.2  christos     /*
    656  1.2  christos      * A match in ~/.k5login is sufficient.  A non-match, falls through to the
    657  1.2  christos      * .k5login.d code below.
    658  1.2  christos      */
    659  1.2  christos     if (ret == 0 && *result == TRUE) {
    660  1.2  christos 	free(path);
    661  1.2  christos 	return 0;
    662  1.1     elric     }
    663  1.2  christos     if (ret != ENOENT)
    664  1.2  christos 	found_file = TRUE;
    665  1.1     elric 
    666  1.2  christos     /*
    667  1.2  christos      * A match in ~/.k5login.d/somefile is sufficient.  A non-match, falls
    668  1.2  christos      * through to the code below that handles negative results.
    669  1.2  christos      *
    670  1.2  christos      * XXX: put back the .d; clever|hackish? you decide
    671  1.2  christos      */
    672  1.2  christos     path[strlen(path)] = '.';
    673  1.2  christos     ret = check_directory(context, path, luser, FALSE, principal, result);
    674  1.2  christos     free(path);
    675  1.2  christos     if (ret == 0 && *result == TRUE)
    676  1.2  christos 	return 0;
    677  1.2  christos     if (ret != ENOENT && ret != ENOTDIR)
    678  1.1     elric 	found_file = TRUE;
    679  1.1     elric 
    680  1.2  christos     /*
    681  1.2  christos      * When either ~/.k5login or ~/.k5login.d/ exists, but neither matches
    682  1.2  christos      * and we're authoritative, we're done.  Otherwise, give other plugins
    683  1.2  christos      * a chance.
    684  1.2  christos      */
    685  1.2  christos     *result = FALSE;
    686  1.2  christos     if (found_file && (flags & KUSEROK_K5LOGIN_IS_AUTHORITATIVE))
    687  1.2  christos 	return 0;
    688  1.2  christos     return KRB5_PLUGIN_NO_HANDLE;
    689  1.2  christos #endif
    690  1.2  christos }
    691  1.1     elric 
    692  1.2  christos static krb5_error_code KRB5_LIB_CALL
    693  1.2  christos kuserok_deny_plug_f(void *plug_ctx, krb5_context context, const char *rule,
    694  1.2  christos 		    unsigned int flags, const char *k5login_dir,
    695  1.2  christos 		    const char *luser, krb5_const_principal principal,
    696  1.2  christos 		    krb5_boolean *result)
    697  1.2  christos {
    698  1.2  christos     if (strcmp(rule, "DENY") != 0)
    699  1.2  christos 	return KRB5_PLUGIN_NO_HANDLE;
    700  1.2  christos 
    701  1.2  christos     *result = FALSE;
    702  1.2  christos     return 0;
    703  1.2  christos }
    704  1.1     elric 
    705  1.2  christos static krb5_error_code KRB5_LIB_CALL
    706  1.2  christos kuser_ok_null_plugin_init(krb5_context context, void **ctx)
    707  1.2  christos {
    708  1.2  christos     *ctx = NULL;
    709  1.2  christos     return 0;
    710  1.2  christos }
    711  1.1     elric 
    712  1.2  christos static void KRB5_LIB_CALL
    713  1.2  christos kuser_ok_null_plugin_fini(void *ctx)
    714  1.2  christos {
    715  1.2  christos     return;
    716  1.1     elric }
    717  1.2  christos 
    718  1.2  christos static krb5plugin_kuserok_ftable kuserok_simple_plug = {
    719  1.2  christos     KRB5_PLUGIN_KUSEROK_VERSION_0,
    720  1.2  christos     kuser_ok_null_plugin_init,
    721  1.2  christos     kuser_ok_null_plugin_fini,
    722  1.2  christos     kuserok_simple_plug_f,
    723  1.2  christos };
    724  1.2  christos 
    725  1.2  christos static krb5plugin_kuserok_ftable kuserok_sys_k5login_plug = {
    726  1.2  christos     KRB5_PLUGIN_KUSEROK_VERSION_0,
    727  1.2  christos     kuser_ok_null_plugin_init,
    728  1.2  christos     kuser_ok_null_plugin_fini,
    729  1.2  christos     kuserok_sys_k5login_plug_f,
    730  1.2  christos };
    731  1.2  christos 
    732  1.2  christos static krb5plugin_kuserok_ftable kuserok_user_k5login_plug = {
    733  1.2  christos     KRB5_PLUGIN_KUSEROK_VERSION_0,
    734  1.2  christos     kuser_ok_null_plugin_init,
    735  1.2  christos     kuser_ok_null_plugin_fini,
    736  1.2  christos     kuserok_user_k5login_plug_f,
    737  1.2  christos };
    738  1.2  christos 
    739  1.2  christos static krb5plugin_kuserok_ftable kuserok_deny_plug = {
    740  1.2  christos     KRB5_PLUGIN_KUSEROK_VERSION_0,
    741  1.2  christos     kuser_ok_null_plugin_init,
    742  1.2  christos     kuser_ok_null_plugin_fini,
    743  1.2  christos     kuserok_deny_plug_f,
    744  1.2  christos };
    745  1.2  christos 
    746