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

1.1       misho       1: /*
1.1.1.4 ! misho       2:  * Copyright (c) 2010-2014 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: #include <ctype.h>
                     49: #include <errno.h>
                     50: #include <fcntl.h>
                     51: #include <limits.h>
                     52: #include <grp.h>
                     53: #include <pwd.h>
                     54: 
                     55: #include "missing.h"
1.1.1.4 ! misho      56: #include "sudo_dso.h"
        !            57: #include "sudo_plugin.h"
        !            58: #include "sudo_util.h"
1.1       misho      59: 
                     60: /*
                     61:  * Sudoers group plugin that does group name-based lookups using the system
                     62:  * group database functions, similar to how sudo behaved prior to 1.7.3.
                     63:  * This can be used on systems where lookups by group ID are problematic.
                     64:  */
                     65: 
                     66: static sudo_printf_t sudo_log;
                     67: 
                     68: typedef struct group * (*sysgroup_getgrnam_t)(const char *);
                     69: typedef struct group * (*sysgroup_getgrgid_t)(gid_t);
                     70: typedef void (*sysgroup_gr_delref_t)(struct group *);
                     71: 
                     72: static sysgroup_getgrnam_t sysgroup_getgrnam;
                     73: static sysgroup_getgrgid_t sysgroup_getgrgid;
                     74: static sysgroup_gr_delref_t sysgroup_gr_delref;
                     75: static bool need_setent;
                     76: 
                     77: static int
                     78: sysgroup_init(int version, sudo_printf_t sudo_printf, char *const argv[])
                     79: {
                     80:     void *handle;
                     81: 
                     82:     sudo_log = sudo_printf;
                     83: 
                     84:     if (GROUP_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
                     85:        sudo_log(SUDO_CONV_ERROR_MSG,
                     86:            "sysgroup_group: incompatible major version %d, expected %d\n",
                     87:            GROUP_API_VERSION_GET_MAJOR(version),
                     88:            GROUP_API_VERSION_MAJOR);
                     89:        return -1;
                     90:     }
                     91: 
                     92:     /* Share group cache with sudo if possible. */
1.1.1.4 ! misho      93:     handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_getgrnam");
1.1       misho      94:     if (handle != NULL) {
                     95:        sysgroup_getgrnam = (sysgroup_getgrnam_t)handle;
                     96:     } else {
                     97:        sysgroup_getgrnam = (sysgroup_getgrnam_t)getgrnam;
                     98:        need_setent = true;
                     99:     }
                    100: 
1.1.1.4 ! misho     101:     handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_getgrgid");
1.1       misho     102:     if (handle != NULL) {
                    103:        sysgroup_getgrgid = (sysgroup_getgrgid_t)handle;
                    104:     } else {
                    105:        sysgroup_getgrgid = (sysgroup_getgrgid_t)getgrgid;
                    106:        need_setent = true;
                    107:     }
                    108: 
1.1.1.4 ! misho     109:     handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_gr_delref");
1.1       misho     110:     if (handle != NULL)
                    111:        sysgroup_gr_delref = (sysgroup_gr_delref_t)handle;
                    112: 
                    113:     if (need_setent)
                    114:        setgrent();
                    115: 
                    116:     return true;
                    117: }
                    118: 
                    119: static void
                    120: sysgroup_cleanup(void)
                    121: {
                    122:     if (need_setent)
                    123:        endgrent();
                    124: }
                    125: 
                    126: /*
                    127:  * Returns true if "user" is a member of "group", else false.
                    128:  */
                    129: static int
                    130: sysgroup_query(const char *user, const char *group, const struct passwd *pwd)
                    131: {
1.1.1.4 ! misho     132:     char **member;
1.1       misho     133:     struct group *grp;
                    134: 
                    135:     grp = sysgroup_getgrnam(group);
                    136:     if (grp == NULL && group[0] == '#' && group[1] != '\0') {
1.1.1.4 ! misho     137:        const char *errstr;
        !           138:        gid_t gid = atoid(group + 1, NULL, NULL, &errstr);
        !           139:        if (errstr == NULL)
        !           140:            grp = sysgroup_getgrgid(gid);
1.1       misho     141:     }
                    142:     if (grp != NULL) {
1.1.1.4 ! misho     143:        if (grp->gr_mem != NULL) {
        !           144:            for (member = grp->gr_mem; *member != NULL; member++) {
        !           145:                if (strcasecmp(user, *member) == 0) {
        !           146:                    if (sysgroup_gr_delref)
        !           147:                        sysgroup_gr_delref(grp);
        !           148:                    return true;
        !           149:                }
1.1       misho     150:            }
                    151:        }
                    152:        if (sysgroup_gr_delref)
                    153:            sysgroup_gr_delref(grp);
                    154:     }
                    155: 
                    156:     return false;
                    157: }
                    158: 
1.1.1.3   misho     159: __dso_public struct sudoers_group_plugin group_plugin = {
1.1       misho     160:     GROUP_API_VERSION,
                    161:     sysgroup_init,
                    162:     sysgroup_cleanup,
                    163:     sysgroup_query
                    164: };

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