Annotation of embedaddon/sudo/plugins/system_group/system_group.c, revision 1.1.1.3

1.1       misho       1: /*
1.1.1.3 ! misho       2:  * Copyright (c) 2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       misho       3:  *
                      4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  */
                     16: 
                     17: #include <config.h>
                     18: 
                     19: #include <sys/types.h>
                     20: #include <sys/stat.h>
                     21: 
                     22: #include <stdio.h>
                     23: #ifdef STDC_HEADERS
                     24: # include <stdlib.h>
                     25: # include <stddef.h>
                     26: #else
                     27: # ifdef HAVE_STDLIB_H
                     28: #  include <stdlib.h>
                     29: # endif
                     30: #endif /* STDC_HEADERS */
                     31: #ifdef HAVE_STDBOOL_H
                     32: # include <stdbool.h>
                     33: #else
                     34: # include "compat/stdbool.h"
                     35: #endif /* HAVE_STDBOOL_H */
                     36: #ifdef HAVE_STRING_H
                     37: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
                     38: #  include <memory.h>
                     39: # endif
                     40: # include <string.h>
                     41: #endif /* HAVE_STRING_H */
                     42: #ifdef HAVE_STRINGS_H
                     43: # include <strings.h>
                     44: #endif /* HAVE_STRINGS_H */
                     45: #ifdef HAVE_UNISTD_H
                     46: # include <unistd.h>
                     47: #endif /* HAVE_UNISTD_H */
                     48: #ifdef HAVE_DLOPEN
                     49: # include <dlfcn.h>
                     50: #else
                     51: # include "compat/dlfcn.h"
                     52: #endif
                     53: #include <ctype.h>
                     54: #include <errno.h>
                     55: #include <fcntl.h>
                     56: #include <limits.h>
                     57: #include <grp.h>
                     58: #include <pwd.h>
                     59: 
                     60: #include "sudo_plugin.h"
                     61: #include "missing.h"
                     62: 
                     63: #ifndef RTLD_DEFAULT
                     64: # define RTLD_DEFAULT  NULL
                     65: #endif
                     66: 
                     67: /*
                     68:  * Sudoers group plugin that does group name-based lookups using the system
                     69:  * group database functions, similar to how sudo behaved prior to 1.7.3.
                     70:  * This can be used on systems where lookups by group ID are problematic.
                     71:  */
                     72: 
                     73: static sudo_printf_t sudo_log;
                     74: 
                     75: typedef struct group * (*sysgroup_getgrnam_t)(const char *);
                     76: typedef struct group * (*sysgroup_getgrgid_t)(gid_t);
                     77: typedef void (*sysgroup_gr_delref_t)(struct group *);
                     78: 
                     79: static sysgroup_getgrnam_t sysgroup_getgrnam;
                     80: static sysgroup_getgrgid_t sysgroup_getgrgid;
                     81: static sysgroup_gr_delref_t sysgroup_gr_delref;
                     82: static bool need_setent;
                     83: 
                     84: static int
                     85: sysgroup_init(int version, sudo_printf_t sudo_printf, char *const argv[])
                     86: {
                     87:     void *handle;
                     88: 
                     89:     sudo_log = sudo_printf;
                     90: 
                     91:     if (GROUP_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
                     92:        sudo_log(SUDO_CONV_ERROR_MSG,
                     93:            "sysgroup_group: incompatible major version %d, expected %d\n",
                     94:            GROUP_API_VERSION_GET_MAJOR(version),
                     95:            GROUP_API_VERSION_MAJOR);
                     96:        return -1;
                     97:     }
                     98: 
                     99:     /* Share group cache with sudo if possible. */
                    100:     handle = dlsym(RTLD_DEFAULT, "sudo_getgrnam");
                    101:     if (handle != NULL) {
                    102:        sysgroup_getgrnam = (sysgroup_getgrnam_t)handle;
                    103:     } else {
                    104:        sysgroup_getgrnam = (sysgroup_getgrnam_t)getgrnam;
                    105:        need_setent = true;
                    106:     }
                    107: 
                    108:     handle = dlsym(RTLD_DEFAULT, "sudo_getgrgid");
                    109:     if (handle != NULL) {
                    110:        sysgroup_getgrgid = (sysgroup_getgrgid_t)handle;
                    111:     } else {
                    112:        sysgroup_getgrgid = (sysgroup_getgrgid_t)getgrgid;
                    113:        need_setent = true;
                    114:     }
                    115: 
1.1.1.2   misho     116:     handle = dlsym(RTLD_DEFAULT, "sudo_gr_delref");
1.1       misho     117:     if (handle != NULL)
                    118:        sysgroup_gr_delref = (sysgroup_gr_delref_t)handle;
                    119: 
                    120:     if (need_setent)
                    121:        setgrent();
                    122: 
                    123:     return true;
                    124: }
                    125: 
                    126: static void
                    127: sysgroup_cleanup(void)
                    128: {
                    129:     if (need_setent)
                    130:        endgrent();
                    131: }
                    132: 
                    133: /*
                    134:  * Returns true if "user" is a member of "group", else false.
                    135:  */
                    136: static int
                    137: sysgroup_query(const char *user, const char *group, const struct passwd *pwd)
                    138: {
                    139:     char **member, *ep = '\0';
                    140:     struct group *grp;
                    141: 
                    142:     grp = sysgroup_getgrnam(group);
                    143:     if (grp == NULL && group[0] == '#' && group[1] != '\0') {
                    144:        long lval = strtol(group + 1, &ep, 10);
                    145:        if (*ep == '\0') {
                    146:            if ((lval != LONG_MAX && lval != LONG_MIN) || errno != ERANGE)
                    147:                grp = sysgroup_getgrgid((gid_t)lval);
                    148:        }
                    149:     }
                    150:     if (grp != NULL) {
                    151:        for (member = grp->gr_mem; *member != NULL; member++) {
                    152:            if (strcasecmp(user, *member) == 0) {
                    153:                if (sysgroup_gr_delref)
                    154:                    sysgroup_gr_delref(grp);
                    155:                return true;
                    156:            }
                    157:        }
                    158:        if (sysgroup_gr_delref)
                    159:            sysgroup_gr_delref(grp);
                    160:     }
                    161: 
                    162:     return false;
                    163: }
                    164: 
1.1.1.3 ! misho     165: __dso_public struct sudoers_group_plugin group_plugin = {
1.1       misho     166:     GROUP_API_VERSION,
                    167:     sysgroup_init,
                    168:     sysgroup_cleanup,
                    169:     sysgroup_query
                    170: };

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>