File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / sudo / plugins / sample_group / sample_group.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:26:49 2012 UTC (12 years, 3 months ago) by misho
Branches: sudo, MAIN
CVS tags: v1_8_5p1, HEAD
sudo 1.8.5p1

    1: /*
    2:  * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
    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/param.h>
   21: #include <sys/stat.h>
   22: 
   23: #include <stdio.h>
   24: #ifdef STDC_HEADERS
   25: # include <stdlib.h>
   26: # include <stddef.h>
   27: #else
   28: # ifdef HAVE_STDLIB_H
   29: #  include <stdlib.h>
   30: # endif
   31: #endif /* STDC_HEADERS */
   32: #ifdef HAVE_STDBOOL_H
   33: # include <stdbool.h>
   34: #else
   35: # include "compat/stdbool.h"
   36: #endif /* HAVE_STDBOOL_H */
   37: #ifdef HAVE_STRING_H
   38: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
   39: #  include <memory.h>
   40: # endif
   41: # include <string.h>
   42: #endif /* HAVE_STRING_H */
   43: #ifdef HAVE_STRINGS_H
   44: # include <strings.h>
   45: #endif /* HAVE_STRINGS_H */
   46: #ifdef HAVE_UNISTD_H
   47: # include <unistd.h>
   48: #endif /* HAVE_UNISTD_H */
   49: #include <ctype.h>
   50: #include <errno.h>
   51: #include <fcntl.h>
   52: #include <limits.h>
   53: #include <grp.h>
   54: #include <pwd.h>
   55: 
   56: #include "sudo_plugin.h"
   57: #include "missing.h"
   58: 
   59: /*
   60:  * Sample sudoers group plugin that uses an extra group file with the
   61:  * same format as /etc/group.
   62:  */
   63: 
   64: static sudo_printf_t sudo_log;
   65: 
   66: extern void mysetgrfile(const char *);
   67: extern void mysetgrent(void);
   68: extern void myendgrent(void);
   69: extern struct group *mygetgrnam(const char *);
   70: 
   71: static int
   72: sample_init(int version, sudo_printf_t sudo_printf, char *const argv[])
   73: {
   74:     struct stat sb;
   75: 
   76:     sudo_log = sudo_printf;
   77: 
   78:     if (GROUP_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
   79: 	sudo_log(SUDO_CONV_ERROR_MSG,
   80: 	    "sample_group: incompatible major version %d, expected %d\n",
   81: 	    GROUP_API_VERSION_GET_MAJOR(version),
   82: 	    GROUP_API_VERSION_MAJOR);
   83: 	return -1;
   84:     }
   85: 
   86:     /* Sanity check the specified group file. */
   87:     if (argv == NULL || argv[0] == NULL) {
   88: 	sudo_log(SUDO_CONV_ERROR_MSG,
   89: 	    "sample_group: path to group file not specified\n");
   90: 	return -1;
   91:     }
   92:     if (stat(argv[0], &sb) != 0) {
   93: 	sudo_log(SUDO_CONV_ERROR_MSG,
   94: 	    "sample_group: %s: %s\n", argv[0], strerror(errno));
   95: 	return -1;
   96:     }
   97:     if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
   98: 	sudo_log(SUDO_CONV_ERROR_MSG,
   99: 	    "%s must be only be writable by owner\n", argv[0]);
  100: 	return -1;
  101:     }
  102: 
  103:     mysetgrfile(argv[0]);
  104:     mysetgrent();
  105: 
  106:     return true;
  107: }
  108: 
  109: static void
  110: sample_cleanup(void)
  111: {
  112:     myendgrent();
  113: }
  114: 
  115: /*
  116:  * Returns true if "user" is a member of "group", else false.
  117:  */
  118: static int
  119: sample_query(const char *user, const char *group, const struct passwd *pwd)
  120: {
  121:     struct group *grp;
  122:     char **member;
  123: 
  124:     grp = mygetgrnam(group);
  125:     if (grp != NULL) {
  126: 	for (member = grp->gr_mem; *member != NULL; member++) {
  127: 	    if (strcasecmp(user, *member) == 0)
  128: 		return true;
  129: 	}
  130:     }
  131: 
  132:     return false;
  133: }
  134: 
  135: struct sudoers_group_plugin group_plugin = {
  136:     GROUP_API_VERSION,
  137:     sample_init,
  138:     sample_cleanup,
  139:     sample_query
  140: };

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